Android RelativeLayout和子元素,layout_width ="match_parent"

jua*_*uan 4 android parent match width relativelayout

当我在同一个"行"有两个视图时,Android如何计算视图的大小,一个宽度="fill_parent"?

例:

<RelativeLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_height="fill_parent"  
    android:layout_width="fill_parent">  
    <EditText  
        android:id="@+id/EditText01"  
        android:hint="Enter some text..."  
        android:layout_alignParentLeft="true"  
        android:layout_width="match_parent"  
        android:layout_toLeftOf="@+id/Button01"  
        android:layout_height="wrap_content"></EditText>  
    <Button  
        android:id="@+id/Button01"  
        android:text="Press Here!"  
        android:layout_width="wrap_content"  
        android:layout_alignParentRight="true"  
        android:layout_height="wrap_content"></Button>  
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

此代码为EditText提供所有可用空间,并在其右侧显示按钮.但是,通过此更改,EditText将填充所有宽度,按钮不在屏幕上:

<RelativeLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_height="fill_parent"  
    android:layout_width="fill_parent">  
    <EditText  
        android:id="@+id/EditText01"  
        android:hint="Enter some text..."  
        android:layout_alignParentLeft="true"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"></EditText>  
    <Button  
        android:id="@+id/Button01"  
        android:text="Press Here!"  
        android:layout_width="wrap_content"  
    android:layout_toRightOf="@+id/EditText01"
        android:layout_height="wrap_content"></Button>  
</RelativeLayout
Run Code Online (Sandbox Code Playgroud)

Tro*_*omB 7

android:layout_toLeftOf="@+id/Button01"将强制右边界EditText对齐到左侧Button,因此Android忽略match_parent宽度,迫使宽度仅从按钮的左侧父侧填充到右侧.

android:layout_toRightOf="@+id/EditText01"将迫使结合的左Button对齐到的右侧EditText,但是由于EditTextIS match_parent宽度权利侧对准父视图的右侧和Android只会迫使按钮在屏幕上.