我有一个线性布局(水平方向),其中包含3个按钮。我想要3个按钮有一个固定的宽度,并在线性布局的宽度上均匀分布。

我可以通过将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:layout_weight属性

其他回答

最好的方法是使用TableLayout与android:layout_width="match_parent"和在列中使用android:layout_weight="1"为所有列。

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

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

([]表示用于间距的线性布局对象)

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

希望这能有所帮助。

宽度和be均匀分布在布局的宽度上。为什么不使用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>```

现代的解决方案是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>

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

Flexbox功能强大得多;它是ConstraintLayout的一个很好的补充。这是一个很好的学习资源。

你应该使用一个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);

}