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

当前回答

我创建了一个自定义的View DistributeLayout来做到这一点。

其他回答

好吧,如果你正好有3个按钮,如果它是ok(甚至计划),外层按钮对齐到左边和右边,那么你可能想尝试一个相对布局,这是更少的开销(在许多情况下)。

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

这将在不同的方向和屏幕尺寸上工作得很好。

如果你不想让按钮缩放,但是调整按钮之间的间距(所有按钮之间的间距相等),你可以使用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>

你可以通过给两个视图一个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>

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

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

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

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