是否可以在 android 线性布局的宽度上均匀分布按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3470420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:19:37  来源:igfitidea点击:

is it possible to evenly distribute buttons across the width of an android linearlayout

androidandroid-linearlayout

提问by yamspog

I have a linear layout (oriented horizontally) that contains 3 buttons. I want the 3 buttons to have a fixed width and be evenly distributed across the width of the linear layout.

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

I can manage this by setting the gravity of the linearlayout to center and then adjusting the padding of the buttons, but this works for a fixed width and won't work for changing devices or orientations.

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

<LinearLayout android:id="@+id/LinearLayout01" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:gravity="center">

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

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


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

</LinearLayout>

回答by Dan Dyer

Expanding on fedj's answer, if you set layout_widthto 0dpand set the layout_weightfor each of the buttons to 1, the available width will be shared equally between the buttons.

扩大对fedj的答案,如果你设置layout_width0dp并设置layout_weight每个按钮为1,可用宽度将被分享按钮之间平分。

回答by stoefln

If you don't want the buttons to scale, but adjust the spacing between the buttons (equal spacing between all buttons), you can use views with weight="1" which will fill the space between the buttons:

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

回答by Sandhu

You may use it with like the following.

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

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

回答by Joop

You can do this by giving both Views a layout_widthof 0dpand a layout_weightof 1:

您可以通过同时提供Viewsa layout_widthof0dp和 a layout_weightof来做到这一点1

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

The way android layout_weight works is that:

android layout_weight 的工作方式是:

  • first, it looks to the size that a View would normally take and reserves this space.
  • second, if the layout is match_parentthen it will divide the space that is leftin the ratio of the layout_weights. Thus if you gave the Views layout_weight="2"and layout_weight="1",the resultant ratio will be 2 to 1,that is : the first View will get 2/3 of the space that is left and the other view 1/3.
  • 首先,它查看视图通常占用的大小并保留此空间。
  • 其次,如果布局是match_parent那么它将以s的比例划分剩余空间layout_weight。因此,如果您给 Viewslayout_weight="2"layout_weight="1",结果比率将是 2 比 1,即:第一个 View 将获得剩余空间的 2/3,另一个 View 将获得 1/3。

So that's why if you give layout_widtha size of 0dpthe first step has no added meaning since both Views are not assigned any space. Then only the second point decides the space each Viewgets, thus giving the Views the space you specified according to the ratio!

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

To explain why 0dpcauses the space to devide equally by providing an example that shows the opposite:The code below would result in something different since example textnow has a width that is greater than 0dpbecause it has wrap_contentinstead making the free space left to divide less than 100% because the text takes space. The result will be that they do get 50% of the free space left but the text already took some space so the TextViewwill have well over 50%of the total space.

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>

回答by marsbear

Well, if you have exactly 3 buttons and if it is ok (or even planned) that the outer buttons are aligned to the left and right side then you might want to try a RelativeLayout which is less overhead (in many situations).

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

You can use layout_alignParentBottomto align all buttons with the bottom of the layout. Use layout_alignParentLeft and Rightfor the outer buttons and layout_centerHorizontalfor the middle button.

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

That will work well on different orientations and screen sizes.

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

回答by fedj

You should take a look to android:layout_weight attribute

你应该看看 android:layout_weight 属性

回答by Gibolt

The modern solution for this is Flexbox.

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

Make sure to import implementation 'com.google.android:flexbox:2.0.0'

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

Flexboxis far more powerful; it is a good complement to ConstraintLayout. This is a great resourceto learn more.

Flexbox更强大;它是对 的一个很好的补充ConstraintLayout这是了解更多信息的绝佳资源

Difference between space_around, space_between, and space_evenly

space_around、space_between 和 space_evenly 之间的区别

回答by martyglaubitz

i created a custom View DistributeLayoutto do this.

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

回答by Muhammad

For evenly spacing out two buttons in a horizontal linear layout, I used 3 LinearLayout objects to act as spaces which are going to be automatically resized. I positioned these LinearLayout objects as follow:

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

[] Button1 [] Button2 []

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

([] represents a LinearLayout object used for spacing)

([] 代表一个用于间距的 LinearLayout 对象)

then I set each of these [] LinearLayout objects' weights to 1, and I get evenly spaced out buttons.

然后我将这些 [] LinearLayout 对象的权重设置为 1,并且我得到了均匀分布的按钮。

Hope this helps.

希望这可以帮助。

回答by matdev

I suggest you use LinearLayout's weightSum attribute.

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

Adding the tag android:weightSum="3"to your LinearLayout's xml declaration and then android:layout_weight="1"to your Buttons will result in the 3 buttons being evenly distributed.

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