我有一个线性布局(水平方向),其中包含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>

当前回答

<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厘米。如果你想让它触屏,把它作为汤姆和蒂姆得到假设为1厘米,这意味着他们结合虚拟,但其2D平面在底部。这显示在屏幕上。

其他回答

你可以通过给两个视图一个0dp的layout_width和1的layout_weight来做到这一点:

<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比率中剩下的空间。因此,如果你给视图layout_weight="2"和layout_weight="1",结果的比例将是2比1,也就是说:第一个视图将得到剩下的2/3的空间,另一个视图将得到1/3的空间。

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

为了解释为什么0dp会导致空间等分,提供了一个相反的例子:下面的代码会导致一些不同的结果,因为示例文本现在的宽度大于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>

另一种解决方案是将按钮嵌套在约束布局中,并以这种方式等距放置它们。确保适当地设置约束,这样按钮将在布局中均匀间隔。

即使你有一个线性布局作为根。

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/deleteButton"
        android:layout_width="wrap_content"
        android:layout_height="64dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:text="asdf"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/shareButton"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/shareButton"
        android:layout_width="wrap_content"
        android:layout_height="64dp"

        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:text="sdsfa"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/deleteButton"
        app:layout_constraintTop_toTopOf="@+id/deleteButton" />
</androidx.constraintlayout.widget.ConstraintLayout>

你应该看看android:layout_weight属性

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

android:text=""

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

android:paddingBottom="250dp"

或者你需要什么都行。 这里有一个例子。

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

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

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

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

希望这能有所帮助。