【问题标题】:split LinearLayout via percent [closed]通过百分比拆分 LinearLayout [关闭]
【发布时间】:2014-01-03 12:16:18
【问题描述】:

我想按百分比制作这样的 LinearLayout

每个矩形都是一个线性布局

这可能吗?怎么做?

【问题讨论】:

标签: android android-linearlayout


【解决方案1】:

检查此示例 xml...这将有助于理解 LinearLayout 中的 weightSumlayout_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" />

【讨论】:

  • 对每个按钮使用 android:layout_width="0dp"。如果方向是水平的。
  • 哎呀对不起!!忘记了!!谢谢:)
  • @Namecan'tbedisplayed 下次请将该问题标记为重复。我理解您愿意回答和提供帮助的意愿,但是当您在其他地方看到满意的答案时,请尽量让事情井井有条,正如您自己的答案在链接到它时所说的那样。
【解决方案2】:

使用这个:

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-07
      • 2012-09-28
      • 2016-12-30
      相关资源
      最近更新 更多