是否可以在 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
is it possible to evenly distribute buttons across the width of an android 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_width
to 0dp
and set the layout_weight
for each of the buttons to 1, the available width will be shared equally between the buttons.
扩大对fedj的答案,如果你设置layout_width
到0dp
并设置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 View
s a layout_width
of 0dp
and a layout_weight
of 1
:
您可以通过同时提供View
sa layout_width
of0dp
和 a layout_weight
of来做到这一点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_parent
then it will divide the space that is leftin the ratio of thelayout_weight
s. Thus if you gave the Viewslayout_weight="2"
andlayout_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_width
a size of 0dp
the first step has no added meaning since both Views are not assigned any space. Then only the second point decides the space each View
gets, thus giving the View
s the space you specified according to the ratio!
所以这就是为什么如果你给第一步layout_width
的大小0dp
没有额外的意义,因为两个视图都没有分配任何空间。那么只有第二个点决定每个人View
获得的空间,从而View
根据比例给s 指定的空间!
To explain why 0dp
causes the space to devide equally by providing an example that shows the opposite:The code below would result in something different since example text
now has a width that is greater than 0dp
because it has wrap_content
instead 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 TextView
will 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_alignParentBottom
to align all buttons with the bottom of the layout. Use layout_alignParentLeft and Right
for the outer buttons and layout_centerHorizontal
for 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'
Flexbox
is far more powerful; it is a good complement to ConstraintLayout
. This is a great resourceto learn more.
回答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 个按钮均匀分布。