Search code examples
androidandroid-layoutandroid-custom-view

custom view layout_gravity not working when called on another layout


I created a custom view and when I use it on another layout the layout_gravity on the ImageView is not working.

heres the code

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:clickable="false">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="@dimen/text_size_semi_large"
        android:clickable="false"
        android:visibility="gone"/>
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2dp"
        app:cardPreventCornerOverlap="true"
        app:cardCornerRadius="2dp"
        android:clickable="true"
        android:focusable="true"
        android:foreground="?attr/selectableItemBackground">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="@drawable/card_background_white"
            android:padding="@dimen/activity_padding">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginRight="@dimen/drawable_padding"
                android:layout_marginEnd="@dimen/drawable_padding" />
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.8"
                android:textColor="@color/warm_gray"
                android:textSize="@dimen/text_size_semi_large"
                android:text="Two line \n Text"/>
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginStart="@dimen/drawable_padding"
                android:layout_marginLeft="@dimen/drawable_padding"
                android:src="@drawable/ic_arrow_right_material"/>
        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

this is the output

and this is the output when I use the custom view in another layout


Solution

  • Try this Showed Fine For me

    <?xml version="1.0" encoding="utf-8"?>
    <merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:clickable="false">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/text_size_semi_large"
            android:clickable="false"
            android:visibility="gone"/>
        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="2dp"
            app:cardPreventCornerOverlap="true"
            app:cardCornerRadius="2dp"
            android:clickable="true"
            android:focusable="true"
            android:foreground="?attr/selectableItemBackground">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center_vertical"
                android:background="@drawable/card_background_white"
                android:padding="@dimen/activity_padding">
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginRight="@dimen/drawable_padding"
                    android:layout_marginEnd="@dimen/drawable_padding" />
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.8"
                    android:textColor="@color/warm_gray"
                    android:textSize="@dimen/text_size_semi_large"
                    android:text="Two line \n Text"/>
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginStart="@dimen/drawable_padding"
                    android:layout_marginLeft="@dimen/drawable_padding"
                    android:src="@drawable/ic_arrow_right_material"/>
            </LinearLayout>
        </android.support.v7.widget.CardView>
    </LinearLayout>