Android 中的线性布局和权重

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2698817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:00:19  来源:igfitidea点击:

Linear Layout and weight in Android

androidandroid-linearlayoutandroid-layout-weight

提问by Janusz

I always read about this funny weight value in the Android documentations. Now I want to try it for the first time but it isn't working at all.

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

As I understand it from the documentations this layout:

正如我从文档中所理解的这种布局:

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

should create two buttons that are horizontally aligned and share the space equally. The problem is the two buttons don't grow to fill the space.

应该创建两个水平对齐并平均共享空间的按钮。问题是两个按钮不会增长以填充空间。

I would like the buttons to grow and fill the whole line. If both buttons are set to match parent only the first button is shown and fills the whole line.

我希望按钮能够增长并填满整条线。如果两个按钮都设置为匹配父级,则仅显示第一个按钮并填充整行。

采纳答案by JeremyFromEarth

You are not setting the layout_weightproperty. Your code reads weight="1"and it should read android:layout_weight="1".

您没有设置该layout_weight属性。您的代码读取weight="1",它应该读取android:layout_weight="1"

回答by Anke

3 things to remember:

3件事要记住:

  • set the android:layout_widthof the children to "0dp"
  • set the android:weightSumof the parent (edit:as Jason Moore noticed, this attribute is optional, because by default it is set to the children's layout_weight sum)
  • set the android:layout_weightof each child proportionally (e.g. weightSum="5", three children: layout_weight="1", layout_weight="3", layout_weight="1")
  • 将孩子的android:layout_width设置为“0dp”
  • 设置父级的android:weightSum编辑:正如 Jason Moore 注意到的,这个属性是可选的,因为默认情况下它被设置为孩子的 layout_weight sum)
  • 按比例设置每个孩子的android:layout_weight(例如 weightSum="5",三个孩子:layout_weight="1", layout_weight="3", layout_weight="1")

Example:

例子:

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

And the result:

结果:

Layout weight example

布局权重示例

回答by Manoj Seelan

It's android:layout_weight. Weight can only be used in LinearLayout. If the orientation of linearlayout is Vertical, then use android:layout_height="0dp"and if the orientation is horizontal, then use android:layout_width = "0dp". It'll work perfectly.

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

回答by Cheezy Code

This image summarizes the Linear layout.

此图像总结了线性布局。

Linear Layout and Weight

线性布局和权重

You can follow this link for more information on the topic. Just Maths - Views, View Groups and Layouts

您可以点击此链接以获取有关该主题的更多信息。Just Math - 视图、视图组和布局

Video Tutorial For Linear Layout : Width, Height & Weights

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

Android Linear Layout Tutorial

Android 线性布局教程

回答by jqpubliq

Try setting the layout_widthof both buttons to "0dip" and the weightof both buttons to 0.5

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

回答by Rakesh Soni

LinearLayout supports assigning a weight to individual children. This attribute assigns an "importance" value to a view, and allows it to expand to fill any remaining space in the parent view. Default weight is zero

LinearLayout 支持为单个子项分配权重。此属性为视图分配一个“重要性”值,并允许它扩展以填充父视图中的任何剩余空间。默认权重为零

calculation to assign any Remaining/Extraspace between child. (not the total space)

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

space assign to child = (child individual weight) / (sum of weight of every child in Linear Layout)

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

Example (1):if there are three text boxes and two of them declare a weight of 1, while the third one is given no weight (0), then Remaining/Extraspace assign to

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

1st text box = 1/(1+1+0) 
2nd text box = 1/(1+1+0) 
3rd text box = 0/(1+1+0) 

Example (2): let's say we have a text label and two text edit elements in a horizontal row. The label has no layout_weight specified, so it takes up the minimum space required to render. If the layout_weight of each of the two text edit elements is set to 1, the remaining width in the parent layout will be split equally between them (because we claim they are equally important).

示例(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)

If the first one text box has a layout_weight of 1 and the second text box has a layout_weight of 2, then one third of the remaining space will be given to the first, and two thirds to the second (because we claim the second one is more important).

如果第一个文本框的 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) 

回答by Green goblin

In the width field of button, replace wrap-contentwith 0dp.
Use layout_weight attribute of a view.

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

android:layout_width="0dp"  

This is how your code will look like:

这是您的代码的样子:

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

layout_weight is used to distribute the whatever left space into proportions. In this case, the two buttons are taking "0dp" width. So, the remaining space will be divided into 1:1 proportion among them, i.e. the space will be divided equally between the Button Views.

layout_weight 用于将剩余空间按比例分配。在这种情况下,两个按钮的宽度为“0dp”。因此,剩余的空间将按 1:1 的比例在它们之间进行划分,即在 Button Views 之间平均划分空间。

回答by ahmed hamdy

Like answer of @Manoj Seelan

喜欢@Manoj Seelan的回答

Replace android:layout_weightWith android:weight.

替换android:layout_weightandroid:weight

When you use Weightwith LinearLayout. you must add weightSumin LinearLayoutand according to orientation of your LinearLayoutyou must setting 0dpfor Width/Height to all LinearLayout`s Children views

当您将WeightLinearLayout. 你必须添加weightSumLinearLayout,并根据你的方向LinearLayout,你必须设置0dp所有的宽度/高度LinearLayout'的孩子意见

Example :

例子 :

IfThe orientation of Linearlayoutis Vertical, then Set Width of all LinearLayout`s Children views with 0dp

如果的方向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>

Ifthe orientation Linearlayoutof is horizontal, then Set Height of all LinearLayout`s Children views with 0dp.

如果的方向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>

回答by JeremyFromEarth

Perhaps setting both of the buttons layout_width properties to "fill_parent" will do the trick.

也许将两个按钮 layout_width 属性设置为“fill_parent”就可以了。

I just tested this code and it works in the emulator:

我刚刚测试了这段代码,它在模拟器中工作:

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

Be sure to set layout_width to "fill_parent" on both buttons.

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

回答by Yar

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/logonFormButtons"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="true"       
        android:orientation="horizontal">

        <Button
            android:id="@+id/logonFormBTLogon"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"            
            android:text="@string/logon"
            android:layout_weight="0.5" />

        <Button
            android:id="@+id/logonFormBTCancel"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"            
            android:text="@string/cancel"
            android:layout_weight="0.5" />
    </LinearLayout>