Android中的线性布局和重量

Jan*_*usz 251 android android-linearlayout android-layout-weight

我总是在Android文档中读到这个有趣的重量值.现在我想第一次尝试它,但它根本不工作.

据我所知,这个布局的文件:

  <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal">

     <Button
        android:text="Register"
        android:id="@+id/register"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dip"
        weight="1" />

     <Button
        android:text="Not this time"
        android:id="@+id/cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dip"
        weight="1" />

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

应创建两个水平对齐的按钮,并平等分享空间.问题是两个按钮不会增长以填充空间.

我想按钮增长并填满整行.如果两个按钮都设置为仅匹配父按钮,则显示第一个按钮并填充整行.

Ank*_*nke 665

要记住的3件事:

  • 将孩子的android:layout_width设置为"0dp"
  • 设置父级的android:weightSum(编辑:正如Jason Moore注意到的,这个属性是可选的,因为默认情况下它被设置为孩子们的layout_weight总和)
  • 按比例设置每个孩子的android:layout_weight(例如weightSum ="5",三个孩子:layout_weight ="1",layout_weight ="3",layout_weight ="1")

例:

    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:weightSum="5">

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="1" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:text="2" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="3" />

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

结果如下:

布局重量示例

  • 感谢关于将宽度设置为零的提示.我还发现不需要在父级上设置weightSum. (63认同)
  • 很高兴知道,谢谢.如果是这样,当您不希望子项填充父项的100%时,设置weightSum仍然有用. (27认同)
  • 这对于静态XML布局是正确的.如果你在运行时动态添加Views,你需要使用带有布局参数的addView,例如`addView(button,new LinearLayout.LayoutParams(0,height,1));`即使你在扩充布局也是如此具有正确的宽度和重量值. (13认同)

Jer*_*rth 152

您没有设置该layout_weight属性.您的代码读取weight="1"并且应该读取android:layout_weight="1".


小智 52

是的android:layout_weight.重量只能用于LinearLayout.如果linearlayout的方向是Vertical,则使用android:layout_height="0dp",如果方向是水平的,则使用android:layout_width = "0dp".它会很完美.


Che*_*ode 25

此图像总结了线性布局.

线性布局和重量

您可以点击此链接获取有关该主题的更多信息.只是数学 - 视图,视图组和布局

线性布局的视频教程:宽度,高度和重量

Android线性布局教程


jqp*_*liq 16

尝试将layout_width两个按钮设置为"0dip",并将weight两个按钮设置为0.5


Rak*_*oni 7

LinearLayout支持为各个孩子分配权重.此属性为视图指定" 重要性 "值,并允许它展开以填充父视图中的任何剩余空间.默认权重为零

计算以分配孩子之间的任何剩余/额外空间.(不是总空间)

空间分配给孩子=(儿童个人体重)/(线性布局中每个孩子的体重总和)

示例(1): 如果有三个文本框,其中两个声明权重为1,而第三个没有赋予权重(0),则剩余/额外空间分配给

1st text box = 1/(1+1+0) 
2nd text box = 1/(1+1+0) 
3rd text box = 0/(1+1+0) 
Run Code Online (Sandbox Code Playgroud)

示例(2):假设我们在水平行中有一个文本标签和两个文本编辑元素.标签没有指定layout_weight,因此它占用了渲染所需的最小空间.如果两个文本编辑元素中每个文本编辑元素的layout_weight设置为1,则父布局中的剩余宽度将在它们之间平均分配(因为我们声称它们同样重要).

calculation : 
1st label = 0/(0+1+1) 
2nd text box = 1/(0+1+1) 
3rd text box = 1/(0+1+1)
Run Code Online (Sandbox Code Playgroud)

如果第一个文本框的layout_weight为1且第二个文本框的layout_weight为2,则剩余空间的三分之一将被赋予第一个,而三分之二将被赋予第二个(因为我们声称第二个是更重要).

calculation : 
1st label = 0/(0+1+2) 
2nd text box = 1/(0+1+2) 
3rd text box = 2/(0+1+2) 
Run Code Online (Sandbox Code Playgroud)


Gre*_*lin 7

在按钮的宽度字段中,替换wrap-content0dp.
使用视图的layout_weight属性.

android:layout_width="0dp"  
Run Code Online (Sandbox Code Playgroud)

这就是你的代码的样子:

<LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">

 <Button
    android:text="Register"
    android:id="@+id/register"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:layout_weight="1" />

 <Button
    android:text="Not this time"
    android:id="@+id/cancel"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:layout_weight="1" />    

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

layout_weight用于将任何左侧空间分配为比例.在这种情况下,两个按钮的宽度为"0dp".因此,剩余空间将被分为1:1比例,即空间将在按钮视图之间平均分配.


ahm*_*mdy 6

就像@Manoj Seelan的回答一样

替换android:layout_weightandroid:weight.

当你使用WeightLinearLayout.你必须添加weightSumLinearLayout,并根据你的方向LinearLayout,你必须设置0dp所有的宽度/高度LinearLayout单曲儿童的意见

示例:

如果方向LinearlayoutVertical,则设置所有LinearLayout子视图的宽度0dp

 <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical"
     android:weightSum="3">

     <Button
        android:text="Register"
        android:id="@+id/register"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:layout_weight="2" />

     <Button
        android:text="Not this time"
        android:id="@+id/cancel"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:layout_weight="1" />

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

如果方向Linearlayouthorizontal,则设置所有LinearLayout儿童视图的高度0dp.

 <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     android:weightSum="3">

     <Button
        android:text="Register"
        android:id="@+id/register"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:padding="10dip"
        android:layout_weight="2" />

     <Button
        android:text="Not this time"
        android:id="@+id/cancel"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:padding="10dip"
        android:layout_weight="1" />

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


Jer*_*rth 5

也许将两个按钮的layout_width属性都设置为“ fill_parent”可以解决问题。

我刚刚测试了这段代码,它可以在模拟器中运行:

<LinearLayout android:layout_width="fill_parent"
          android:layout_height="wrap_content">

    <Button android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="hello world"/>

    <Button android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="goodbye world"/>

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

确保在两个按钮上都将layout_width设置为“ fill_parent”。