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布局