Button half width of screen

后端 未结 4 1535
无人共我
无人共我 2020-12-14 14:31

I have a single button in a linear layout. I want the button to take up half the width of its parent. Is there a way to do this in the layout xml.



        
相关标签:
4条回答
  • 2020-12-14 15:03

    Yep, the solution is very similar to that question, but you also want to set the weightSum of the parent LinearLayout:

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_horizontal"
        android:padding="6dp"
        android:weightSum="2">
    
        <Button
            android:id="@+id/buttonCollect"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="@string/hello"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:layout_weight="1" />
    
    </LinearLayout> 
    
    0 讨论(0)
  • 2020-12-14 15:07

    Exactly the same as the question you posted, just use a dummy view instead:

    <View 
    android:weight="1"
    android:layout_width="0dp"
    android:layout_height="match_parent"/>
    
    0 讨论(0)
  • 2020-12-14 15:11

    You need to set a 0.5 (half) weight, and set the width to 0.

    <Button
        android:id="@+id/buttonCollect"
        android:layout_width="0dip"
        android:layout_weight="0.5"
        android:layout_height="match_parent"
        android:text="@string/hello"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"/>
    
    0 讨论(0)
  • 2020-12-14 15:13

    None of these solutions worked for me on Android L. I had to do wrap_content as below:

    <Button
        android:layout_gravity="center"
        android:id="@+id/btnSignin"
        android:layout_width="wrap_content"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:layout_height="wrap_content"
        android:text="@string/btn_signin"
        android:layout_marginBottom="@dimen/activity_vertical_margin" />
    
    0 讨论(0)
提交回复
热议问题