Set two buttons to same width regardless of screen size?

前端 未结 9 2367
[愿得一人]
[愿得一人] 2020-12-24 00:56

Ok, i have two buttons in linear layout:



        
相关标签:
9条回答
  • 2020-12-24 01:30
    android:layout_weight="0.5"
    android:layout_width="0dp
    

    it's working

    0 讨论(0)
  • 2020-12-24 01:31
    <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.

    0 讨论(0)
  • 2020-12-24 01:33

    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>
    
    0 讨论(0)
  • 2020-12-24 01:35

    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

    0 讨论(0)
  • 2020-12-24 01:41

    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.

    0 讨论(0)
  • 2020-12-24 01:41

    set to each button:

    android:layout_weight="0.5"
    android:layout_width="0dp"
    
    0 讨论(0)
提交回复
热议问题