Android 中 RelativeLayout 的视图宽度和高度始终为零
View width and height is always zero in RelativeLayout in Android
我正在开发 Android 应用程序。在我的应用程序中,我试图在图像上设置叠加层。但是即使我将 match_parent 设置为宽度和高度,相对布局中视图的大小始终为零和高度。
这是我的 XML 布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_width="match_parent"
android:id="@+id/di_card_container"
android:layout_height="wrap_content">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/di_iv_image"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:id="@+id/di_name_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:layout_centerInParent="true"
android:background="@color/blue"
android:id="@+id/destination_item_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:padding="10dp"
android:textSize="15dp"
android:textColor="@color/white"
android:id="@+id/di_tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
这是结果
如您所见,名称没有蓝色背景,因为相对布局中视图的宽度和高度为零。但是,如果我像下面这样明确设置宽度和高度,它就可以工作。
<View
android:layout_centerInParent="true"
android:background="@color/blue"
android:id="@+id/destination_item_overlay"
android:layout_width="300dp"
android:layout_height="30dp"/>
但它并不兼容所有设备。为什么 with 和 height 总是零?我添加该视图是因为我需要以编程方式控制该视图。如何设置该背景视图的宽度和高度以匹配父视图?
那是因为你有循环依赖。 <RelativeLayout>
要求它的 child 告诉它高度,而 <View>
告诉它的 parent 让它和他一样高。如果我们修复高度,我们可以立即解决问题。
在这里试试这个:
<RelativeLayout
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:id="@+id/di_name_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:layout_centerInParent="true"
android:background="@color/blue"
android:id="@+id/destination_item_overlay"
android:layout_width="match_parent"
android:layout_alightTop="@id/di_tv_name"
android:layout_alightBottom="@id/di_tv_name"
android:layout_height="match_parent"/>
<TextView
android:padding="10dp"
android:textSize="15dp"
android:textColor="@color/white"
android:id="@+id/di_tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
我正在开发 Android 应用程序。在我的应用程序中,我试图在图像上设置叠加层。但是即使我将 match_parent 设置为宽度和高度,相对布局中视图的大小始终为零和高度。
这是我的 XML 布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_width="match_parent"
android:id="@+id/di_card_container"
android:layout_height="wrap_content">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/di_iv_image"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:id="@+id/di_name_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:layout_centerInParent="true"
android:background="@color/blue"
android:id="@+id/destination_item_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:padding="10dp"
android:textSize="15dp"
android:textColor="@color/white"
android:id="@+id/di_tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
这是结果
如您所见,名称没有蓝色背景,因为相对布局中视图的宽度和高度为零。但是,如果我像下面这样明确设置宽度和高度,它就可以工作。
<View
android:layout_centerInParent="true"
android:background="@color/blue"
android:id="@+id/destination_item_overlay"
android:layout_width="300dp"
android:layout_height="30dp"/>
但它并不兼容所有设备。为什么 with 和 height 总是零?我添加该视图是因为我需要以编程方式控制该视图。如何设置该背景视图的宽度和高度以匹配父视图?
那是因为你有循环依赖。 <RelativeLayout>
要求它的 child 告诉它高度,而 <View>
告诉它的 parent 让它和他一样高。如果我们修复高度,我们可以立即解决问题。
在这里试试这个:
<RelativeLayout
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:id="@+id/di_name_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:layout_centerInParent="true"
android:background="@color/blue"
android:id="@+id/destination_item_overlay"
android:layout_width="match_parent"
android:layout_alightTop="@id/di_tv_name"
android:layout_alightBottom="@id/di_tv_name"
android:layout_height="match_parent"/>
<TextView
android:padding="10dp"
android:textSize="15dp"
android:textColor="@color/white"
android:id="@+id/di_tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>