ChatGPT解决这个技术问题 Extra ChatGPT

Android中的线性布局和权重

我总是在 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>

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

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

更新:android 百分比支持也可以很好地做到这一点。 code2concept.blogspot.in/2015/08/…

S
Shubham Suryavanshi

3件事要记住:

将孩子的 android:layout_width 设置为“0dp”

设置父级的 android:weightSum (编辑:正如 Jason Moore 所注意到的,此属性是可选的,因为默认情况下它设置为子级的 layout_weight 总和)

按比例设置每个孩子的 android:layout_weight (eg 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>

结果:

https://i.stack.imgur.com/yew2A.png


感谢您提供有关将宽度设置为零的提示。我还发现不需要在父级上设置 weightSum。
很高兴知道,谢谢。如果是这样,当您不希望子代填充父代的 100% 时,设置 weightSum 仍然很有用。
这对于静态 XML 布局是正确的。如果您在运行时动态添加视图,则需要将 addView 与 addView(button, new LinearLayout.LayoutParams(0, height, 1)); 等布局参数一起使用,即使您使用正确的宽度和权重值对布局进行膨胀也是如此。
您说设置父级的 android:weightSum - 将其设置为某个值或将其放入,应该如何设置?
@batbrat 是正确的,我正在动态生成 UI。我有时将 XML 片段视为模板,然后在运行时修改或填充。在这种情况下,这个技巧对我不起作用,我必须重新设置显式宽度和重量才能让它工作。
M
MasterScrat

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


G
Geobits

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


P
Prags

此图像总结了线性布局。

https://i.stack.imgur.com/s5K9I.png

您可以点击此链接以获取有关该主题的更多信息。 Just Maths - Views, View Groups and Layouts

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

Android Linear Layout Tutorial


A
Aditya Vyas-Lakhan

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


现在两个按钮都从屏幕上消失了
好的,然后将布局宽度设置为 fill_parent 和 weights 为 0.5
查看 thisthis。我有点困惑为什么这仍然不起作用,但也许这会给你一些想法。
R
Rakesh Soni

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) 

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

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

G
Green goblin

在按钮的宽度字段中,将 wrap-content 替换为 0dp
使用视图的 layout_weight 属性。

android:layout_width="0dp"  

这就是您的代码的样子:

<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 用于将剩余空间按比例分配。在这种情况下,两个按钮采用“0dp”宽度。因此,剩余空间将在它们之间按 1:1 的比例划分,即空间将在 Button View 之间平均分配。


C
Community

喜欢@Manoj Seelan的答案

android:layout_weight 替换为 android:weight

当您将 WeightLinearLayout 一起使用时。您必须在 LinearLayout 中添加 weightSum,并且根据 LinearLayout 的方向,您必须将 0dp 设置为所有 LinearLayout 的子视图的宽度/高度

例子 :

如果 Linearlayout 的方向是 Vertical ,则使用 0dp 设置所有 LinearLayout 的子视图的宽度

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

如果的方向 Linearlayouthorizontal ,则使用 0dp 设置所有 LinearLayout 的子视图的高度。

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

J
JeremyFromEarth

也许将两个按钮的 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>

请务必在两个按钮上将 layout_width 设置为“fill_parent”。


这只会将右侧按钮推出屏幕并仅显示第一个按钮。
Y
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>

现在我宁愿建议使用 layout_weight="50" 和 layout_width="0px"
A
Akshay Chopra

以下是代码中的更改(以粗体标记):

<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" //changes made here
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:layout_weight="1" /> //changes made here

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

  </LinearLayout>

由于您的 LinearLayout 的方向为水平,因此您只需将宽度保持为 0dp。在那个方向上使用权重。 (如果你的方向是垂直的,你的高度只会保持 0dp)。

由于有 2 个视图并且您为两个视图都放置了 android:layout_weight="1",这意味着它将在水平方向(或按宽度)等分两个视图。


A
Aman Alam

在上述 XML 中,将线性布局的 android:layout_weight 设置为 2android:layout_weight="2"


为什么需要这个?为什么2的布局权重很重要?为什么不是 20 或 200?
a
ahmed hamdy

此外,您需要为 LinerLayout 的子视图 [按钮视图] 添加此 android:layout_width="0dp"


D
Dinesh Saini

你必须这样写它为我工作

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

         <Button
            android:text="Register"
            android:id="@+id/register"
            android:layout_width="wrap_content"
            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="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dip"
            android:layout_weight="1" />

K
Katrina Khan
 <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="2"
        android:text="Button 1" />

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

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

    </LinearLayout>

V
Vaibhav Jani

这是您问题的完美答案

  <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        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>

那么它是“重量”还是“布局重量”?
它是 android:layout_weight
a
ahmed hamdy

wrap_content 替换为 fill_parent


m
mirazimi
 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:background="#008">

            <RelativeLayout
                android:id="@+id/paneltamrin"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"

                >
                <Button
                    android:id="@+id/BtnT1"
                    android:layout_width="wrap_content"
                    android:layout_height="150dp"
                    android:drawableTop="@android:drawable/ic_menu_edit"
                    android:drawablePadding="6dp"
                    android:padding="15dp"
                    android:text="AndroidDhina"
                    android:textColor="#000"
                    android:textStyle="bold" />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/paneltamrin2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                >
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="150dp"
                     android:drawableTop="@android:drawable/ic_menu_edit"
                    android:drawablePadding="6dp"
                    android:padding="15dp"
                    android:text="AndroidDhina"
                    android:textColor="#000"
                    android:textStyle="bold" />

            </RelativeLayout>
        </LinearLayout>

https://i.stack.imgur.com/YCaJb.png


A
Adeeksha Sathsara

这里有一些例子

等重的水平方向

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">


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

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

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

</LinearLayout>

https://i.stack.imgur.com/ZTEmr.png

权重不等的水平方向

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">


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

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

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

</LinearLayout>

https://i.stack.imgur.com/ejDNS.png

等重垂直方向

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

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

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

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

</LinearLayout>

https://i.stack.imgur.com/kN9UJ.png

希望对您有所帮助!