Set two buttons to same width regardless of screen size?

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

Ok, i have two buttons in linear layout:



        
相关标签:
9条回答
  • 2020-12-24 01:45
    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

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

    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"

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

    You can set them in linear layout nested in relative layout like this

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:orientation="vertical">
    
    
    
            <Button
                android:id="@+id/bestBtn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:layout_marginBottom="5dp"
                android:textAllCaps="false"
                android:textSize="16sp"
                android:textColor="@color/grey_light2"
                android:background="@drawable/sharefb_btn"
                android:text="@string/sharefb" />
            <Button
                android:id="@+id/playBtn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/grey_light2"
                android:textAllCaps="false"
                android:textSize="16sp"
                android:layout_marginBottom="50dp"
                android:textStyle="bold"
                android:background="@drawable/puzzle_opening_btn_ncvtelen_3"
                android:text="@string/playagain" />
    
    </LinearLayout>
    

    and you will get somethiing like that

    0 讨论(0)
提交回复
热议问题