Android 在 LinearLayout 中放置 3 个按钮以占用相等的空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11669060/
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 08:25:49  来源:igfitidea点击:

Place 3 buttons in a LinearLayout to occupy equal amount of space

androidandroid-layout

提问by user1527152

i would like to have three buttons taking equal amount of available space horizontally in a row. I used android:layout_gravity. What is the problem?

我希望三个按钮在水平方向上占用等量的可用空间。我用过android:layout_gravity。问题是什么?

layout xml :

布局xml:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="1.0"
    >

    <Button android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Bg"
            android:background="@drawable/button_red"
            android:layout_weight=".2"
            android:layout_gravity="left"
    />
    <Button android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Bm"
            android:background="@drawable/button_red"
            android:layout_weight=".2"
            android:textColor="#ffffff"
            android:layout_gravity="center"
    />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_red"
        android:layout_weight=".2"
        android:text="@string/Bf"
        android:layout_gravity="right"
    />

</LinearLayout>

if someone see whats wrong, thanks.

如果有人看到出了什么问题,谢谢。

回答by Ronnie

The following layout should work. weights are for LinearLayouts..

以下布局应该有效。权重用于 LinearLayouts ..

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3"
>

    <Button android:id="@+id/button1"
            ...
            android:layout_weight="1"/>

    <Button android:id="@+id/button2"
            ...
            android:layout_weight="1"/>

    <Button
        android:id="@+id/button3"
        ...
        android:layout_weight="1"/>

</LinearLayout>

回答by jimbocorn

besides of setting a layout_weightyou have to set layout_widthor layout_heightto 0dp. So if you want for example to distribute the buttons horizontally , layout_widthshould be 0dp and layout_weight.2 or any number as long as is equal through the buttons you have.

除了设置的layout_weight,你必须设置layout_widthlayout_height0dp。因此,例如,如果您想水平分布按钮,layout_width则应为 0dp 和layout_weight0.2 或任何数字,只要与您拥有的按钮相等即可。

so your layout should be like this

所以你的布局应该是这样的

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>

<Button android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/Bg"
        android:background="@drawable/button_red"
        android:layout_weight="1"
/>
<Button android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/Bm"
        android:background="@drawable/button_red"
        android:layout_weight="1"
        android:textColor="#ffffff"
/>

<Button
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@drawable/button_red"
    android:layout_weight="1"
    android:text="@string/Bf"
/>
</LinearLayout>

回答by moDev

Divide the weight sum to equal proportion in all buttons.

将所有按钮中的权重总和等分。

Remove layout_gravity property and add android:layout_weight=0.33.

移除 layout_gravity 属性并添加 android:layout_weight=0.33。

It will work

它会工作

回答by AkashG

Check out this:

看看这个:

<RelativeLayout 
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <Button 
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"/>
    <RelativeLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true">
        <Button 
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/button1"/>
    </RelativeLayout>
    <Button 
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"/>
</RelativeLayout>

回答by ShineDown

Put the buttons in a relative layout. add properties to the buttons allignParentleft = true, another allignParentcenter= true and the allignParentRight = true to each button.

将按钮放在相对布局中。为每个按钮添加属性 allignParentleft = true、另一个 allignParentcenter= true 和 allignParentRight = true。

回答by Dibyendu Mitra Roy

Please consider the following layout snippet:

请考虑以下布局片段:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="3">

    <ImageView
        android:src="@drawable/logo1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="left|center_vertical" />

    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Some Title"
        android:textColor="@android:color/black"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_weight="1" />

    <ImageView
        android:src="@drawable/logo2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="right|center_vertical" />

</LinearLayout>

2 things to note above.

以上2点要注意。

A. I created a LinearLayout with weightSum of 3.

A. 我创建了一个 weightSum 为 3 的 LinearLayout。

B. Then inside that I create 3 elements each having layout_weight of 1 so that I can have my child elements distribute the entire space among themselves evenly.

B. 然后在里面我创建了 3 个元素,每个元素的 layout_weight 为 1,这样我就可以让我的子元素均匀地分布整个空间。

回答by Rajesh Nasit

I have make it pragmatically without weight

我在没有重量的情况下做到了务实

 float width = CommonUtills.getScreenWidth(activity);
    int cardWidth = (int) CommonUtills.convertDpToPixel(((width) / 3), activity);

    LinearLayout.LayoutParams params =
        new LinearLayout.LayoutParams(cardWidth,
            LinearLayout.LayoutParams.MATCH_PARENT);

    btnOne.setLayoutParams(params);
    btnTwo.setLayoutParams(params);
    btnThree.setLayoutParams(params);

    public class CommonUtills {
        public static float getScreenWidth(Context context) {
            float width = (float) 360.0;
            DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
            width = displayMetrics.widthPixels / displayMetrics.density;
            return width;
        }
    }



    <LinearLayout
    android: layout_width = "match_parent"
    android: layout_height = "50dp"    
    android: orientation = "horizontal" >


        <Button
    android: id = "@+id/btnOne"
    android: layout_width = "wrap_content"
    android: layout_height = "match_parent" > </Button>


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



        <Button
    android: id = "@+id/btnThree"
    android: layout_width = "wrap_content"
    android: layout_height = "wrap_content" > </Button> 
        </LinearLayout>

回答by Sahan Pasindu Nirmal

Give android:layout_weight="1"for each Button

android:layout_weight="1"每个按钮

Here Android Developer Guide:

这里是 Android 开发者指南:

Layout Weight

LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view. Child views can specify a weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. Default weight is zero.

For example, if there are three text fields and two of them declare a weight of 1, while the other is given no weight, the third text field without weight will not grow and will only occupy the area required by its content. The other two will expand equally to fill the space remaining after all three fields are measured. If the third field is then given a weight of 2 (instead of 0), then it is now declared more important than both the others, so it gets half the total remaining space, while the first two share the rest equally.

版面权重

LinearLayout 还支持使用 android:layout_weight 属性为单个子项分配权重。这个属性根据它应该在屏幕上占据多少空间为一个视图分配一个“重要性”值。较大的权重值允许它扩展以填充父视图中的任何剩余空间。子视图可以指定一个权重值,然后视图组中的任何剩余空间按照其声明的权重的比例分配给子视图。默认权重为零。

例如,如果有三个文本字段,其中两个声明权重为1,而另一个没有权重,则没有权重的第三个文本字段不会增长,只会占用其内容所需的区域。在测量完所有三个字段后,另外两个将相等地扩展以填充剩余的空间。如果第三个字段的权重为 2(而不是 0),那么它现在被声明为比其他两个更重要,因此它获得总剩余空间的一半,而前两个平均分配其余空间。

Source

来源

回答by Madi

You can divide it like this: `

你可以这样划分它:`

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true"
                android:layout_toLeftOf="@+id/imageButtonLikedPost">

                <ImageButton
                    android:id="@+id/imageButtonMyPost"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:background="@drawable/mypost_tab_bg" />
            </RelativeLayout>

            <ImageButton
                android:id="@+id/imageButtonLikedPost"
                android:layout_width="40dp"
                android:layout_height="30dp"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:background="@drawable/mylike_tab_bg" />

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@+id/imageButtonLikedPost">

                <ImageButton
                    android:id="@+id/imageButtonMyBlog"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:background="@drawable/tab4_bg" />
            </RelativeLayout>
        </RelativeLayout>`