【发布时间】:2011-10-18 21:18:12
【问题描述】:
1.问题
我想要一个有 2 行的全屏线性布局,而第一行分为 2 个部分,其中一个部分占 30%,第二个占宽度的 70%,第二行分为 2 个部分相同的大小(每个宽度的 50%)。此外,第一行仅占屏幕高度的 33%。
我认为使用 android:layout_weight 是正确的方法。但这有点棘手,因为有时它适用于 android:layout_width 有时适用于 android:layout_height 属性,它适用于一个或两个,具体取决于相应 XML 属性的值。
经过实验,我发现如果我将属性值设置为 wrap_content 它会被 android:layout_weight 设置覆盖,例如以下代码将 android:layout_weight 应用于 android:layout_width:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#FF0000"/>
这是一个正确的方法吗?
2。示例
为了贬低布局(见图)我必须这样做:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#FF0000"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#00FF00"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#0000FF"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#0000AA"/>
</LinearLayout>
</LinearLayout>
谢谢 钢
【问题讨论】:
标签: android android-layout android-widget