通过百分比拆分LinearLayout

时间:2014-01-03 12:16:18

标签: android android-linearlayout

我想用百分比制作这样的LinearLayout enter image description here

每个矩形都是LinearLayout

是可以制作的以及如何制作?

3 个答案:

答案 0 :(得分:5)

检查此示例xml ...这有助于了解weightSum

中的layout_weightLinearLayout

选中此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" />

答案 1 :(得分:1)

使用这个:

<?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>

答案 2 :(得分:0)

您需要使每个cell的宽度等于match_parent,然后使用每个孩子的layout_weight值。由于layout_weight指定了多个元素之间的大小比例,即