【问题标题】:Linear Layout and weight in AndroidAndroid中的线性布局和权重
【发布时间】:2011-02-11 12:46:15
【问题描述】:

我总是在 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 android-linearlayout android-layout-weight


【解决方案1】:

要记住的 3 件事:

  • 将孩子的 android:layout_width 设置为 "0dp"
  • 设置父级的android:weightSumedit: 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>

结果:

【讨论】:

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

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

【讨论】:

    【解决方案3】:

    它是android:layout_weight。权重只能用于LinearLayout。如果linearlayout的方向是垂直的,那么使用android:layout_height="0dp",如果方向是水平的,那么使用android:layout_width = "0dp"。它会完美运行。

    【讨论】:

      【解决方案4】:

      这张图片总结了线性布局。

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

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

      Android Linear Layout Tutorial

      【讨论】:

        【解决方案5】:

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

        【讨论】:

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

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

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

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

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

        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) 
        

        【讨论】:

          【解决方案7】:

          在按钮的宽度字段中,将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 之间平均分配。

          【讨论】:

            【解决方案8】:

            喜欢@Manoj Seelan的回答

            android:layout_weight 替换为android:weight

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

            例子:

            如果 Linearlayout 的方向是 Vertical ,则设置所有 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>
            

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

            【讨论】:

              【解决方案9】:

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

              【讨论】:

              • 这只会将右边的按钮推出屏幕并只显示第一个按钮。
              【解决方案10】:
              <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"
              【解决方案11】:

              以下是代码中的更改(标记为 BOLD):

              <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",这意味着它将在水平方向(或按宽度)等分两个视图。

              【讨论】:

                【解决方案12】:

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

                【讨论】:

                • 为什么需要这样做?为什么2的布局权重很重要?为什么不是 20 或 200?
                【解决方案13】:

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

                【讨论】:

                  【解决方案14】:

                  你必须这样写它为我工作

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

                  【讨论】:

                    【解决方案15】:
                     <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>
                    

                    【讨论】:

                      【解决方案16】:

                      这是您问题的完美答案

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

                      【讨论】:

                      • 那么是“weight”还是“layout_weight”??
                      • 是android:layout_weight
                      【解决方案17】:

                      wrap_content 替换为fill_parent

                      【讨论】:

                        【解决方案18】:
                         <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>
                        

                        【讨论】:

                          【解决方案19】:

                          这里有一些例子

                          等重的水平方向

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

                          权重不等的水平方向

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

                          等重的垂直方向

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

                          希望对您有所帮助!

                          【讨论】:

                            猜你喜欢
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            • 2017-06-13
                            • 1970-01-01
                            • 1970-01-01
                            • 2015-04-15
                            • 1970-01-01
                            • 1970-01-01
                            相关资源
                            最近更新 更多