android:相对布局两个按钮占用所有可用的水平空间

nai*_*der 13 android android-layout

我有一个相对布局,我有两个按钮,分别带有文本"hello"和"world".我希望这两个按钮彼此相邻,同样占据整个水平空间.

我尝试了以下但没有得到预期的输出

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">

     <Button android:layout_height="wrap_content" android:layout_width="wrap_content"      android:text="@string/world" android:id="@+id/world"
     android:layout_alignParentRight="true"
     android:layout_toRightOf="@+id/hello"
     android:layout_alignTop="@+id/hello"
  />  


       <Button
          android:id="@+id/hello"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/hello"
          android:layout_alignParentLeft="true"
          android:layout_alignParentBottom="true"
        />
   </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我尝试将两个孩子的android:layout_width更改为fill_parent,但这也没有用.

我有一个使用LinearLayout和layout_weight设置为0.5的两个孩子的工作解决方案,但我想了解是否有相关布局本身的方法.

谢谢,

Wil*_*aan 34

  <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>
Run Code Online (Sandbox Code Playgroud)

校验

注意:

  • 即使android:layout_widthfill_parent结果也不会改变.
  • 这里真正重要的是android:layout_alignParentRightandroid:layout_alignParentLeft


Mat*_*lis 6

你不能这样做RelativeLayout.尝试LinearLayoutlayout_weight属性.


pas*_*k23 5

我知道这是一个旧请求,但也许它对某些人有用。

在您的情况下,正确的解决方案是使用线性布局。但是为了回复您的请求并假设您的布局中有另一个小部件:

<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>
Run Code Online (Sandbox Code Playgroud)