我希望能有3個格,並在屏幕的下半部分像這樣一個矩形:佈局劃分爲規則形狀,儘管屏幕高度
有誰知道如何規範的形狀,從而即使屏幕的垂直高度是長/短,頂部3仍然是正方形,矩形的寬度是正方形的兩倍?我試過使用layout_weight:1.0
,但高度可能會拉長。
我希望不需要注意寬度和高度。
感謝
我希望能有3個格,並在屏幕的下半部分像這樣一個矩形:佈局劃分爲規則形狀,儘管屏幕高度
有誰知道如何規範的形狀,從而即使屏幕的垂直高度是長/短,頂部3仍然是正方形,矩形的寬度是正方形的兩倍?我試過使用layout_weight:1.0
,但高度可能會拉長。
我希望不需要注意寬度和高度。
感謝
您可以使用GridLayout
實現這一目標,因爲它支持columnWeight
和rowWeight
屬性。設置rowWeight
爲1
爲每一行,columnWeight
到1
爲每個正方形和矩形columnSpan
到2
應創建您想要的效果。
要在低於21的API級別上使用此功能,請改爲使用支持版本GridLayout
。
可能是一個矯枉過正的問題,由於性能問題而不推薦使用。但還是:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="0.5"
android:layout_height="0dp">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="0.5"
android:weightSum="2"
android:orientation="vertical"
android:layout_height="0dp">
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="0dp">
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_weight="1"
android:weightSum="3"
android:layout_width="match_parent"
android:layout_height="0dp">
<LinearLayout
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
有很多方法可以做到這一點,你可以使用,如果你想只layout_weight
實現它。
1)通過爲每個設置layout_weight="1"
將第一行分成3個相等大小的視圖。
2)在第二行中,您希望將View設置爲等於2個視圖的大小:Math的值爲2/3 = 0.67。
3)在第二行設置視圖layout_weight="0.67"
。您還必須將剩餘的0.33寬度設置爲不可見視圖。
像這樣:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<View
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<View
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<View
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<View
android:layout_weight="0.67"
android:layout_width="0dp"
android:layout_height="wrap_content"
/>
<View
android:layout_weight="0.33"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:visibility="invisible"/>
</LinearLayout>
可能不建議鳥巢'以這種方式,因爲它會嚴重影響性能LinearLayout's:http://stackoverflow.com/questions/9430764/why-are-nested-重量 - 表現不佳 - 替代品 – PPartisan
是的,不是最優的。但如果佈局不那麼複雜,可以做。 @PPartisan –