【发布时间】:2014-01-03 12:16:18
【问题描述】:
我想按百分比制作这样的 LinearLayout
每个矩形都是一个线性布局
这可能吗?怎么做?
【问题讨论】:
标签: android android-linearlayout
我想按百分比制作这样的 LinearLayout
每个矩形都是一个线性布局
这可能吗?怎么做?
【问题讨论】:
标签: android android-linearlayout
检查此示例 xml...这将有助于理解 LinearLayout 中的 weightSum 和 layout_weight
查看SO
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="100" >
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:text="Button20" />
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:text="Button20" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:text="Button20" />
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="40"
android:text="Button40" />
【讨论】:
使用这个:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100" >
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:text="20%" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:text="20%" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:text="20%" />
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_weight="40"
android:text="40%" />
</LinearLayout>
【讨论】:
您需要使每个cell 的宽度等于match_parent,然后为每个孩子使用layout_weight 值。由于 layout_weight 指定了多个元素之间的大小比例,即
【讨论】: