是否可以在 LinearLayout 的宽度上均匀分布按钮 [英] Is it possible to evenly distribute buttons across the width of a LinearLayout

查看:60
本文介绍了是否可以在 LinearLayout 的宽度上均匀分布按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a LinearLayout (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 LinearLayout.

我可以通过将 LinearLayout 的重力设置为居中然后调整按钮的内边距来管理这一点,但这适用于固定宽度,不适用于不同的设备或方向.

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 different devices or orientations.

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

推荐答案

扩展

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.

这篇关于是否可以在 LinearLayout 的宽度上均匀分布按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆