設置我有一個佈局,看起來大致是這樣的:的RelativeLayout無視大小的孩子
<RelativeLayout>
<TextView
layout_width="match_parent"
layout_height="50dp"
layout_alignParentTop="true"
id="@+/A"/>
<TextView
layout_width="match_parent"
layout_height="46dp"
layout_alignParentBottom="true"
id="@+/B"/>
<CustomView
layout_width="wrap_content"
layout_height="wrap_content"
layout_below="@id/A"
layout_above="@id/B"
layout_centerHorizontal="true"/>
</RelativeLayout>
的目的是,A和B應該在頂部和底部的固定高度,CustomView應該採取儘可能多的剩餘空間,因爲它可能會受到其縱橫比的限制。 (請注意,我不能使用,因爲我省略了一些其他孩子的LinearLayout)
下面是一個特定的屏幕尺寸會發生什麼:
- CustomView.onMeasure被調用(AT_MOST 776,AT_MOST 395)
- CustomView調用setMeasuredDimension(285,391)
- CustomView.onMeasure與再次調用(確切285,整整251)
- CustomView調用setMeasuredDimension(參數似乎並不重要)
- CustomView.onLayout調用尺寸285 X 251
我猜頂和底部的制約力的高度是251,但爲什麼不給RelativeLayout的一個CustomView機會,選擇在此基礎上高度約束的寬度?看來RelativeLayout的錯誤行爲假設如果一個孩子可以是285x391,那麼它也可以是285x251。是否有解決方法(除了用另一個自定義視圖替換RelativeLayout)?
我使用minSdkVersion = 14和targetSdkVersion = 21,以防萬一。
我嘗試使用wrap_content/match_parent的高度和centerInParent/centerHorizontal/no gravity的所有組合。對於高度受限的情況,所有產生這個:http://i.imgur.com/vrE0qxa.png。注意aqua背景表明ImageView不是方形的,儘管圖像在ImageView中居中。也許我不能認爲CustomView可以選擇其縱橫比。 – stewbasic