android:layout_weight如何工作?

shi*_*hin 36 android

当我有以下内容时,它显示顶部布局,四种颜色的面积比底部布局面积小得多.

根据此文档,当您添加更多内容时layout_weight,它应该增加区域,但在代码中会减少.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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="fill_parent"
      android:layout_weight="4">
      <TextView
          android:text="red"
          android:gravity="center_horizontal"
          android:background="#aa0000"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_weight="3"/>
      <TextView
          android:text="green"
          android:gravity="center_horizontal"
          android:background="#00aa00"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_weight="1"/>
      <TextView
          android:text="blue"
          android:gravity="center_horizontal"
          android:background="#0000aa"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_weight="1"/>
      <TextView
          android:text="yellow"
          android:gravity="center_horizontal"
          android:background="#aaaa00"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_weight="1"/>
  </LinearLayout>

  <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1">
    <TextView
        android:text="row one"
        android:textSize="15pt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <TextView
        android:text="row two"
        android:textSize="15pt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <TextView
        android:text="row three"
        android:textSize="15pt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <TextView
        android:text="row four"
        android:textSize="15pt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
  </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

kbr*_*ggs 41

我有同样的问题.诀窍是不要对你要设置权重的控件使用"wrap_content"或"fill_parent".而是将layout_height设置为0px(在垂直布局内),然后子控件将按权重值进行比例调整.


ped*_*oca 33

如果你得到错误

错误

可疑大小:这会使视图不可见,可能用于布局......

记得android:orientation在父级中设置正确的参数

  • 非常感谢你! (3认同)
  • 定位部分很重要.谢谢 :) (2认同)

Jan*_*usz 10

如果在LinearLayout中使用fill_parent,布局将占用尽可能多的空间,并且稍后定义的所有布局项将必须处理剩余的空间.

如果将LinearLayouts的高度设置为wrap_content,则权重应按照记录的方式工作.


trg*_*lia 9

我知道这已经很晚了,但希望它可以帮助人们:

android:weightSum在父母身上使用.这是dev文档的链接.

http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:weightSum

使用android:weightSum1.0父,然后用0.x对孩子的看法与android:layout_weight以便他们使用的空间比例.


Rya*_*rad 8

在Janusz所说的基础上,如果你使用fill_parent,你可以设置android:layout_weight来"拆分"多个布局项之间的"完整区域".

layout_weight不会增加区域,它会"增加"到该区域.但它也与父母有关.如果您的父级具有layout_height = fill_parent,并且layout_weight = 0且父级具有相同的兄弟,则将layout_weight = 1设置为其中一个子级不会影响父级.

父母和兄弟姐妹都会占用他们可以填补的可用区域的50%.