【问题标题】:Is it possible to evenly distribute buttons across the width of a LinearLayout是否可以在 LinearLayout 的宽度上均匀分布按钮
【发布时间】:2011-03-29 01:52:30
【问题描述】:

我有一个包含 3 个按钮的 LinearLayout(水平方向)。我希望 3 个按钮具有固定宽度,并均匀分布在 LinearLayout 的宽度上。

我可以通过将LinearLayout 的重力设置为中心然后调整按钮的填充来管理此问题,但这适用于固定宽度,不适用于不同的设备或方向。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal">

    <Button
        android:id="@+id/btnOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="120dp" />

    <Button
        android:id="@+id/btnTwo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="120dp" />


    <Button
        android:id="@+id/btnThree"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="120dp" />
</LinearLayout>

【问题讨论】:

标签: android android-linearlayout


【解决方案1】:

Width 和 be 均匀分布在布局的宽度上。为什么不使用约束布局?

<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent">

 <Button android:id="@+id/btnOne" android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:width="120dip"
  app:layout_constraintTop_toTopOf="parent"
  app:layout_constraintBottom_toBottomOf="parent"
  app:layout_constraintStart_toStartOf="parent"
  app:layout_constraintEnd_toStartOf="@id/btnTwo">
 </Button> 

 <Button android:id="@+id/btnTwo" android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:width="120dip" 
  app:layout_constraintTop_toTopOf="parent" 
  app:layout_constraintBottom_toBottomOf="parent"
  app:layout_constraintEnd_toStartOf="@id/btnThree"
  app:layout_constraintStart_toEndOf="@id/btnOne"></Button> <Button 
  android:id="@+id/btnThree" android:layout_width="wrap_content"    
  android:layout_height="wrap_content" android:width="120dip" 
  app:layout_constraintTop_toTopOf="parent" 
  app:layout_constraintBottom_toBottomOf="parent" 
  app:layout_constraintStart_toEndOf="@id/btnTwo" 
  app:layout_constraintEnd_toEndOf="parent"> 
 </Button>

</androidx.constraintlayout.widget.ConstraintLayout>```

【讨论】:

    【解决方案2】:

    linearLayout 中,不要给Button 本身赋予权重,而是将权重设置为&lt;Space&gt; 查看这不会拉伸Button

        <?xml version="1.0" encoding="utf-8"?>
        <androidx.constraintlayout.widget.ConstraintLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="4"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.955">
    
            <Space
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="1" />
    
            <Button
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:background="@drawable/ic_baseline_arrow_back_24" />
    
            <Space
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="1" />
    
            <Button
                android:id="@+id/captureButton"
                android:layout_width="72dp"
                android:layout_height="72dp"
                android:background="@drawable/ic_round_camera_24" />
    
            <Space
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="1" />
    
            <Button
                android:id="@+id/cameraChangerBtn"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:background="@drawable/ic_round_switch_camera_24" />
    
            <Space
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="1" />
        </LinearLayout>
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    

    当我使用 4 &lt;Space&gt; 时,我在 linear layout 中设置了 android:weightSum="4" 这也是结果 ::

    【讨论】:

      【解决方案3】:

      您应该使用android:weightSum 属性线性布局。给线性布局一个 weightSum 等于布局内的按钮数量,然后设置android:layout_weight="1" 并设置按钮的宽度android:layout_width="0dp" 此外,您可以使用填充和布局边距设置布局样式。

      <LinearLayout android:id="@+id/LinearLayout01"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:gravity="center"
          android:weightSum="3">
          
          <Button
              android:id="@+id/btnOne"
              android:layout_width="0dp"
              android:text="1"
              android:layout_height="wrap_content"
              android:width="120dip"
              android:layout_weight="1"
              android:layout_margin="15dp" 
              />
          
          <Button
              android:id="@+id/btnTwo"
              android:text="2"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:width="120dip"
              android:layout_weight="1"
              android:layout_margin="15dp" />
      
          <Button
              android:id="@+id/btnThree"
              android:text="3"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:width="120dip"
              android:layout_weight="1"
              android:layout_margin="15dp" />
      
      </LinearLayout>
      

      为了动态地做到这一点

      void initiate(Context context){
          LinearLayout parent = new LinearLayout(context);
      
          parent.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
          parent.setWeightSum(3);
          parent.setOrientation(LinearLayout.HORIZONTAL);
      
          AppCompatButton button1 = new AppCompatButton(context);
          button1.setLayoutParams(new LinearLayout.LayoutParams(0 ,LinearLayout.LayoutParams.WRAP_CONTENT,1.0f));
      
          AppCompatButton button2 = new AppCompatButton(context);
          button2.setLayoutParams(new LinearLayout.LayoutParams(0 ,LinearLayout.LayoutParams.WRAP_CONTENT,1.0f));
      
          AppCompatButton button3 = new AppCompatButton(context);
          button3.setLayoutParams(new LinearLayout.LayoutParams(0 ,LinearLayout.LayoutParams.WRAP_CONTENT,1.0f));
          
          parent.addView(button1);
          parent.addView(button2);
          parent.addView(button3);
      
      }
      

      【讨论】:

      • 再次我想要动态
      【解决方案4】:

      你可以用这个。这很容易理解: https://developer.android

      <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="horizontal"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
      
      <TextView
          android:text="Tom"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:textSize="24sp" />
      
      <TextView
          android:text="Tim"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:textSize="24sp" />
      
      <TextView
          android:text="Todd"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:textSize="24sp" />
      
      </LinearLayout>
      

      【讨论】:

        【解决方案5】:

        最佳方法是将TableLayoutandroid:layout_width="match_parent" 一起使用,并在列中对所有列使用android:layout_weight="1"

        【讨论】:

          【解决方案6】:

          如果您希望 3 个按钮具有固定宽度并均匀分布在布局的宽度上...为什么不使用 constraintLayout?

          <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
          
                  <Button
                      android:id="@+id/btnOne"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:width="120dip"
                      app:layout_constraintTop_toTopOf="parent"
                      app:layout_constraintBottom_toBottomOf="parent"
                      app:layout_constraintStart_toStartOf="parent"
                      app:layout_constraintEnd_toStartOf="@id/btnTwo">
                      
                  </Button>
          
                  <Button
                      android:id="@+id/btnTwo"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:width="120dip"
                      app:layout_constraintTop_toTopOf="parent"
                      app:layout_constraintBottom_toBottomOf="parent"
                      app:layout_constraintEnd_toStartOf="@id/btnThree"
                      app:layout_constraintStart_toEndOf="@id/btnOne"></Button>
          
          
                  <Button
                      android:id="@+id/btnThree"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:width="120dip"
                      app:layout_constraintTop_toTopOf="parent"
                      app:layout_constraintBottom_toBottomOf="parent"
                      app:layout_constraintStart_toEndOf="@id/btnTwo"
                      app:layout_constraintEnd_toEndOf="parent">
                      
                  </Button>
          
          </androidx.constraintlayout.widget.ConstraintLayout>
          
          

          【讨论】:

          【解决方案7】:

          现代解决方案是Flexbox

          <com.google.android.flexbox.FlexboxLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:justifyContent="space_around"> <!-- or "space_between", "space_evenly" -->
          
              <Button 
                  android:layout_width="wrap_content" 
                  android:layout_height="wrap_content" 
                  android:width="120dp" />
          
              <Button 
                  android:layout_width="wrap_content" 
                  android:layout_height="wrap_content" 
                  android:width="120dp" />
          
              <Button 
                  android:layout_width="wrap_content" 
                  android:layout_height="wrap_content" 
                  android:width="120dp" />
          </com.google.android.flexbox.FlexboxLayout>
          

          确保导入implementation 'com.google.android:flexbox:2.0.0'

          Flexbox 更强大;它是对ConstraintLayout 的一个很好的补充。 This is a great resource了解更多。

          【讨论】:

          • 感谢您让我(我们)意识到这一点。绝对有用。我使用了“space_around”,当我在 FlexBox 中有 1 或 3 个可见时,按钮很好地居中
          【解决方案8】:

          Equally weighted children

          创建一个线性布局,其中每个孩子使用相同数量的 屏幕上的空间,将每个视图的 android:layout_height 设置为 “0dp”(用于垂直布局)或每个视图的 android:layout_width 到“0dp”(水平布局)。然后设置 android:layout_weight 每个视图的“1”。

          为了在 LinearLayout 视图组中工作,android:layout_widthandroid:layout_height 的属性值需要等于 "match_parent"...

          【讨论】:

            【解决方案9】:

            最简单最快的方法(但不是最好的)是添加一个带有空文本属性的 TextView,像这样

            android:text=""
            

            LinearLayout的背景颜色必须相同,然后你可以使用padding属性,像这样

            android:paddingBottom="250dp"
            

            或任何你需要的。 Here is an example.

            【讨论】:

              【解决方案10】:
              <LinearLayout
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content">
              
              <TextView
                  android:text="Tom"
                  android:layout_width="wrap_content"
                  android:layout_height="200dp"
                  android:textSize="24sp" />
              
              <TextView
                  android:text="Tim"
                  android:layout_width="wrap_content"
                  android:layout_height="200dp"
                  android:textSize="24sp" />
              
              <TextView
                  android:text="Todd"
                  android:layout_width="wrap_content"
                  android:layout_height="200dp"
                  android:textSize="24sp" />
              

              【讨论】:

                【解决方案11】:

                以上所有答案都是正确的,但如果您需要可见和已消失的功能,那么这种务实的方法会很好用

                <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:orientation="horizontal">
                
                        <Button
                            android:id="@+id/btnOne"
                            android:layout_width="120dp"
                            android:layout_height="match_parent"></Button>
                
                        <Button
                            android:id="@+id/btnTwo"
                            android:layout_width="120dp"
                            android:layout_height="match_parent"></Button>
                
                
                        <Button
                            android:id="@+id/btnThree"
                            android:layout_width="120dp"
                            android:layout_height="match_parent"></Button>
                    </LinearLayout>
                
                
                
                 float width=CommonUtills.getScreenWidth(activity);
                            int cardWidth=(int)CommonUtills.convertDpToPixel (((width)/3),activity);
                
                LinearLayout.LayoutParams params =
                                new LinearLayout.LayoutParams(width,
                                        LinearLayout.LayoutParams.MATCH_PARENT);
                
                btnOne.setLayoutParams(params);
                btnTwo.setLayoutParams(params);
                btnThree.setLayoutParams(params);
                
                public class CommonUtills {
                public static float getScreenWidth(Context context) {
                        float width = (float) 360.0;
                        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
                        width = displayMetrics.widthPixels / displayMetrics.density;
                        return width;
                    }
                }
                

                【讨论】:

                  【解决方案12】:
                  <LinearLayout
                      xmlns:android="http://schemas.android.com/apk/res/android"
                      android:orientation="vertical"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent">
                  
                      <TextView
                          android:text="Tom"
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          android:textSize="24sp" 
                          android:layout_weight="3"/>
                  
                      <TextView
                          android:text="Tim"
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          android:textSize="24sp"
                          android:layout_weight="3"/>
                  
                      <TextView
                          android:text="Todd"
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          android:textSize="24sp" 
                          android:layout_weight="3"/>
                  
                  </LinearLayout>
                  

                  在圆圈中,汤姆、蒂姆和托德被假定为 3 厘米。如果您希望它是触摸屏,请将其设置为 Tom 和 Tim 被假定为 1 厘米,这意味着它们结合了虚拟但其 2D 平面位于底部。这会显示在屏幕上。

                  【讨论】:

                    【解决方案13】:

                    以上使用 layout_ 的答案对我不起作用,但以下答案对我有用。

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:layout_weight="0.1"
                        android:layout_gravity="center_horizontal"
                        >
                    
                        <android.support.design.widget.FloatingActionButton
                            android:layout_width="50dp"
                            android:layout_height="50dp"
                            android:layout_gravity="center"
                           />
                    
                        <android.support.design.widget.FloatingActionButton
                            android:layout_width="50dp"
                            android:layout_height="50dp"
                            android:layout_gravity="center"
                            android:layout_marginLeft="40dp"
                            android:layout_marginStart="40dp"/>
                    
                        <android.support.design.widget.FloatingActionButton
                            android:layout_width="50dp"
                            android:layout_height="50dp"
                            android:layout_gravity="center"
                            android:layout_marginLeft="40dp"
                            android:layout_marginStart="40dp"
                            />
                    
                        <android.support.design.widget.FloatingActionButton
                            android:layout_width="50dp"
                            android:layout_height="50dp"
                            android:layout_gravity="center"
                            android:layout_marginLeft="40dp"
                            android:layout_marginStart="40dp"/>
                    
                    </LinearLayout>
                    

                    这就是它在屏幕上的样子,

                    【讨论】:

                      【解决方案14】:

                      您可以通过同时给Views 一个layout_width0dp 和一个layout_weight1 来做到这一点:

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

                      android layout_weight 的工作方式是:

                      • 首先,它查看视图通常占用的大小并保留此空间。
                      • 其次,如果布局是match_parent,那么它会按照layout_weights的比例划分剩余空间。因此,如果您给 Views layout_weight="2"layout_weight="1",则结果比率将为 2 比 1,即:第一个 View 将获得剩余空间的 2/3,而另一个 View 将获得 1/3。李>

                      所以这就是为什么如果你给layout_width 一个大小0dp 第一步没有额外的意义,因为两个视图都没有分配任何空间。然后只有第二点决定了每个View 获得的空间,从而根据比例给Views 你指定的空间!

                      通过提供一个相反的示例来解释为什么0dp 会导致空间平均分配: 下面的代码将导致不同的结果,因为example text 现在具有更大的宽度比0dp,因为它有wrap_content,而是使剩余的可用空间除以小于100%,因为文本占用空间。结果将是他们确实获得了剩余 50% 的可用空间,但文本已经占用了一些空间,因此 TextView 将拥有远远超过 50% 的总空间。

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

                      【讨论】:

                        【解决方案15】:

                        这可以通过将 weight 分配给添加到容器内的每个按钮来实现,这对于定义水平方向非常重要:

                            int buttons = 5;
                        
                            RadioGroup rgp = (RadioGroup) findViewById(R.id.radio_group);
                        
                            rgp.setOrientation(LinearLayout.HORIZONTAL);
                        
                            for (int i = 1; i <= buttons; i++) {
                                RadioButton rbn = new RadioButton(this);         
                                rbn.setId(1 + 1000);
                                rbn.setText("RadioButton" + i);
                                //Adding weight
                                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f);
                                rbn.setLayoutParams(params);
                                rgp.addView(rbn);
                            }
                        

                        所以我们可以在我们的设备中得到这个结果:

                        即使我们旋转我们的设备,每个按钮中定义的 weight 也可以沿容器均匀分布元素:

                        【讨论】:

                          【解决方案16】:

                          如果您不希望按钮缩放,但调整按钮之间的间距(所有按钮之间的间距相等),您可以使用 weight="1" 的视图来填充按钮之间的空间:

                              <Space
                                  android:layout_width="0dp"
                                  android:layout_height="1dp"
                                  android:layout_weight="1" >
                              </Space>
                          
                              <ImageButton
                                  android:layout_width="wrap_content"
                                  android:layout_height="wrap_content"
                                  android:adjustViewBounds="true"
                                  android:background="@null"
                                  android:gravity="center_horizontal|center_vertical"
                                  android:src="@drawable/tars_active" />
                          
                              <Space
                                  android:layout_width="0dp"
                                  android:layout_height="1dp"
                                  android:layout_weight="1" >
                              </Space>
                          
                              <ImageButton
                                  android:layout_width="wrap_content"
                                  android:layout_height="wrap_content"
                                  android:adjustViewBounds="true"
                                  android:background="@null"
                                  android:gravity="center_horizontal|center_vertical"
                                  android:src="@drawable/videos_active" />
                          
                              <Space
                                  android:layout_width="0dp"
                                  android:layout_height="1dp"
                                  android:layout_weight="1" >
                              </Space>
                          

                          【讨论】:

                          • 这绝对是最简单、开销最低的方法(适用于您希望缩放视图之间的空间,而不是视图本身的情况)。我垂直使用它,所以只是交换了宽度和高度值。谢谢。
                          • 这是一个优雅的解决方案。您可能更喜欢使用Space 视图类型。使事情更具可读性。
                          • Ryan,是的,如果您使用的是 API 14+(我们应该这样做)。
                          • 老实说,我认为它在有 4 个按钮时不起作用,但它非常出色,没有大惊小怪和运行时间测量。尊重!
                          • 这比目前被接受为答案的要好。
                          【解决方案17】:

                          我建议你使用 LinearLayout 的 weightSum 属性。

                          添加标签 android:weightSum="3" 到您的 LinearLayout 的 xml 声明,然后 android:layout_weight="1" 到您的按钮将导致 3 个按钮均匀分布。

                          【讨论】:

                            【解决方案18】:

                            你可以像下面这样使用它。

                            <LinearLayout
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:orientation="horizontal"
                                android:layout_marginTop="15dp">
                                <Space
                                    android:layout_weight="1"
                                    android:layout_height="wrap_content"
                                    android:layout_width="wrap_content"/>
                                <Button
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:text="Save"/>
                                <Space
                                    android:layout_weight="1"
                                    android:layout_height="wrap_content"
                                    android:layout_width="wrap_content"/>
                                <Button
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:text="Reset"/>
                                <Space
                                    android:layout_weight="1"
                                    android:layout_height="wrap_content"
                                    android:layout_width="wrap_content"/>
                                <Button
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:text="cancel"/>
                                <Space
                                    android:layout_weight="1"
                                    android:layout_height="wrap_content"
                                    android:layout_width="wrap_content"/>
                            </LinearLayout>
                            

                            【讨论】:

                            • 太棒了!!直到现在我才听说过Space
                            【解决方案19】:

                            我为此创建了一个自定义视图DistributeLayout

                            【讨论】:

                              【解决方案20】:

                              为了在水平线性布局中均匀地间隔开两个按钮,我使用了 3 个 LinearLayout 对象来充当将自动调整大小的空间。我将这些 LinearLayout 对象定位如下:

                              [] 按钮 1 [] 按钮 2 []

                              ([]表示一个LinearLayout对象,用于间距)

                              然后我将每个 [] LinearLayout 对象的权重设置为 1,然后我得到均匀间隔的按钮。

                              希望这会有所帮助。

                              【讨论】:

                                【解决方案21】:

                                好吧,如果您正好有 3 个按钮,并且外部按钮与左右对齐是可以的(甚至计划),那么您可能想尝试开销较小的 RelativeLayout(在许多情况下) .

                                您可以使用layout_alignParentBottom 将所有按钮与布局底部对齐。外部按钮使用layout_alignParentLeft and Right,中间按钮使用layout_centerHorizontal

                                这适用于不同的方向和屏幕尺寸。

                                【讨论】:

                                • 澄清一下,如果您有超过三个按钮,这将不起作用。
                                • 没错。您必须改用 LinearLayout 或 RadioGroup(如果适用)。
                                【解决方案22】:

                                fedj's answer 上展开,如果您将layout_width 设置为0dp 并将每个按钮的layout_weight 设置为1,则可用宽度将在按钮之间平均共享。

                                【讨论】:

                                • 为了扩展这个,如果你不希望按钮的宽度是屏幕的 1/3,将每个按钮包装在一个 LinearLayout 中并设置 layout_width="0dp" 和 layout_weight=" 1" 在 3 个线性布局上。此外,将 LinearLayouts 上的重力设置为“center”,这样按钮将在每个 LinearLayout 的中心对齐。
                                • 这仅适用于线性布局。任何人都可以给出在 relativelayout 中等间距的任何想法
                                • 在我的情况下,我使用 layout_width="wrap_content" 并且只有 layout_weight="1" 效果很好!
                                • 这是不正确的,因为 OP 想要修复。请参阅 stoefln 的回答。
                                • 我们需要相等填充的图像视图呢?无法将权重设置为 imageview,因为图像将被拉伸。
                                【解决方案23】:

                                你应该看看 android:layout_weight 属性

                                【讨论】:

                                • 我的理解是 layout_weight 属性会拉伸按钮的大小以填充布局。我想保持按钮的大小不变,只是增加按钮之间的填充。
                                • 啊!我真的不知道最好的方法。也许在布局中包含每个按钮。 3 具有 layout_weight 属性的布局和布局中居中的按钮。但我真的不知道这是否是最好的方法。
                                猜你喜欢
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                • 2020-10-23
                                • 2012-06-26
                                • 1970-01-01
                                相关资源
                                最近更新 更多