RelativeLayout设置wrap_content无效

尊重劳动成果,转载请标明出处:http://www.cnblogs.com/tangZH/p/8419053.html 

在做项目的过程中,遇到了一个奇怪的现象,我设置RelativeLayout为的宽度为wrap_content,而且RelativeLayout里面的组件也设置了固定大小,可是RelativeLayout宽度大小还是会铺满整个屏幕。 

为啥???一脸闷逼

百度之后发现,官方文档有解释:

A Layout where the positions of the children can be described in relation to each other or to the parent.Note that you cannot have a circular dependency between the size of the RelativeLayout and the position of its children. For example, you cannot have a RelativeLayout whose height is set to WRAP_CONTENT and a child set to ALIGN_PARENT_BOTTOM.

它的意思大概是说:不能在RelativeLayout的大小和子级的位置之间存在循环依赖关系。 例如,不能将RelativeLayout的高度设置为WRAP_CONTENT,将子设置为ALIGN_PARENT_BOTTOM。

就我遇到的两种情况而言是这样的:

1、设置RelativeLayout的宽度为wrap_content,子组件设置为

android:layout_alignParentEnd="true"

于是出现了RelativeLayout宽度铺满屏幕的现象

2、设置RelativeLayout的高度为wrap_content,子组件设置为

ALIGN_PARENT_BOTTOM

于是出现了RelativeLayout的高度铺满屏幕的现象

最终,我还是乖乖改为LinearLayout布局

原文地址:https://www.cnblogs.com/tangZH/p/8419053.html