Set two buttons to same width regardless of screen

2019-02-01 09:45发布

Ok, i have two buttons in linear layout:

<LinearLayout android:id="@+id/linearLayout1" 
              android:layout_height="wrap_content" 
              android:layout_width="fill_parent">
        <Button android:id="@+id/aktiviraj_paket" 
                android:text="Aktiviraj" 
                android:layout_height="40sp" 
                android:layout_width="160sp" 
                android:background="@drawable/my_border3" 
                android:onClick="myClickHandle"></Button>
        <Button android:id="@+id/deaktiviraj_paket" 
                android:text="Deaktiviraj" 
                android:layout_height="40sp" 
                android:layout_width="fill_parent" 
                android:background="@drawable/my_border3"
                android:onClick="myClickHandle">
        </Button>
</LinearLayout>

So the thing is, if I use fill parent on both buttons, they are one on each other, so i have made first button 160sp width, and second is fill_parent. If this is shown on 4 inch screen or smaller, buttons are the same size, but if i try this on tablet (10 inch) first button stays 160sp wide, and second is stretched till the end of screen (because fill_parent). Can i make this, so both buttons could be even size in no matter what size is the screen ??

8条回答
爷的心禁止访问
2楼-- · 2019-02-01 10:12

set to each button:

android:layout_weight="0.5"
android:layout_width="0dp"
查看更多
聊天终结者
3楼-- · 2019-02-01 10:12
<LinearLayout
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:text="One" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:text="Two" />
</LinearLayout>

These is the example for equal size buttons for side by side can be done from above code

android:layout_weight 

is used to assign space for buttons or whatever of equal amount for every child of LinearLayout.

Note: It works only on linear layout.

查看更多
做个烂人
4楼-- · 2019-02-01 10:22

Use android:layout_weight="1" on both Buttons. Set android:layout_width="0dp" on both. Since both buttons now have equal weighting, they will now each have half the parent's width.

You can find out more here: http://developer.android.com/guide/topics/ui/layout/linear.html

查看更多
Melony?
5楼-- · 2019-02-01 10:26
Display display=getWindowManager().getDefaultDisplay();
    int width=display.getWidth();
    btn1.setWidth(width/2);
    btn2.seTwidth(width/2);

Set anything in xml file then first find width of device then set width half to both button Now On every device they will look exactly same

查看更多
做个烂人
6楼-- · 2019-02-01 10:31

If what you're looking to do is to make all the buttons the width of the widest button, setting weights isn't going to do that. Rather, you can put all the buttons in a TableLayout:

   <TableLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/short_text" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/enter_manually" />
        </TableRow>
    </TableLayout>

This layout will show 2 buttons, one on top of the other, the same width.

查看更多
ゆ 、 Hurt°
7楼-- · 2019-02-01 10:32
android:layout_weight="0.5"
android:layout_width="0dp

it's working

查看更多
登录 后发表回答