在另一个布局上调用时,自定义视图layout_gravity不起作用

时间:2016-12-13 14:26:09

标签: android android-layout android-custom-view

我创建了一个自定义视图,当我在另一个布局上使用它时,ImageView上的layout_gravity无效。

继承人的代码

<?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

1 个答案:

答案 0 :(得分:0)

试试这个对我来说很好

<?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>