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条回答
Bombasti
2楼-- · 2019-02-01 10:32

This is working:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
        android:layout_width="0dp"
        android:layout_weight="0.5"
        android:layout_height="wrap_content"
        android:text="my btn 1"/>
    <Button
        android:layout_width="0dp"
        android:layout_weight="0.5"
        android:layout_height="wrap_content"
        android:text="my btn 2"/>
    </LinearLayout>
查看更多
混吃等死
3楼-- · 2019-02-01 10:38

Set android:layout_weight="1" in the containing layout The linear layout should have android:orientation set as horizontal. And then the inside buttons should have the following:

android:layout_width="0dp"

android:layout_weight="0.5"

查看更多
登录 后发表回答