android: relative layout two buttons occupy all the available horizontal space

前端 未结 6 1174
不知归路
不知归路 2021-02-13 11:06

i have a relative layout and i have two buttons in it with texts \"hello\" and \"world\" respectively. i want these two buttons to lie adjacent to each other and equally occupy

相关标签:
6条回答
  • 2021-02-13 11:42

    You may try implementing LinearLayout(horizontal) and then set android:layout_weight="1" for both buttons.

    0 讨论(0)
  • 2021-02-13 11:46
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    
    int height = metrics.heightPixels;
    int width = metrics.widthPixels;
    

    in relative layout you can achieve this dynamically.

    Button1.setWidth(width / 2);
    Button2.setWidth(width / 2);
    
    0 讨论(0)
  • 2021-02-13 11:48

    I know that is a old request, but maybe it's can be useful for somebody.

    In your case the right solution is using Linear Layout. But for reply your request and suppose that you have an other widget in your layout:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
    
     <TextView
        android:id="@+id/tvdescription"        
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:text="description here"/>
    
     <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/tvdescription">
    
        <Button          
            android:id="@+id/world"
            android:layout_height="wrap_content" 
            android:layout_width="0dp"      
            android:text="@string/world"
            android:weight="1"
        />  
       <Button
            android:id="@+id/hello"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/hello"
            android:weight="1"
        />
    </LinearLayout>
    

    0 讨论(0)
  • 2021-02-13 11:48

    Take a Button align it in center with 1dp width and height. Now place your Button1 with the property android:layout_width = fill parent to left of center, again with respect to center button put Button2 to the right of center with the property android:layout_width = fill parent.

    0 讨论(0)
  • 2021-02-13 11:59

    You can't do this with RelativeLayout. Try LinearLayout and the layout_weight attribute.

    0 讨论(0)
  • 2021-02-13 12:04
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <Button
            android:id="@+id/world"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/hello"
            android:layout_toRightOf="@+id/view"
            android:text="First" />
    
        <View
            android:id="@+id/view"
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_centerHorizontal="true" />
    
        <Button
            android:id="@+id/hello"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_toLeftOf="@+id/view"
            android:text="Second" />
    
    </RelativeLayout>
    

    Check

    Note:

    • Even if the android:layout_width was fill_parent the result will not change.
    • What actually matters here is android:layout_alignParentRight and android:layout_alignParentLeft
    0 讨论(0)
提交回复
热议问题