Android RelativeLayout and childs with layout_width=“match_parent”

How Android calculates the size of a View when I have two Views at the same "row", one with width="fill_parent"?

Example:

<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>

This code gives all free space to EditText and show the button to his right. But with this changes the EditText fills all width and the button is out of the screen:

<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

Answers


android:layout_toLeftOf="@+id/Button01" will force the right bound of the EditText to align to the left side of the Button , thus Android ignores the match_parent width forcing the width to only fill from the left parent side to the right side of the button.

android:layout_toRightOf="@+id/EditText01" will force the left bound of the Button to align to the right side of the EditText, but since the EditText is match_parent width the rights side is aligned to the right side of the parent view and Android will just force the button off the screen.


In both of your examples, <EditText/> fits all the width of screen. In the first example, the Button is not dependent on <EditText/> , so it can align to right edge of screen. In the second example, however, Button has to align to the right of `, that's why it gets pushed out of view.


If you are using a RelativeLayout then you have no "rows", all you have is a space where you can arrange the views. If you want to keep the button in the right part of the screen and have the EditText fill the rest of the screen you could do something like this:

<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_toLeftOf="@+id/Button01"
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"></EditText>  
    <Button  
        android:id="@+id/Button01"  
        android:text="Press Here!" 
        android:alignParentRight="true"
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"></Button>  
</RelativeLayout/>

Need Your Help

How to achieve this effect seen as seen in the Hotel Tonight app

ios ios6 uiscrollview core-animation

I am trying to figure out how a couple of pieces of this effect are done. The effect is the transition from a scroll view of images to what looks like a UIViewController with paging enabled.

grails spring security login is not working

grails spring-security passwords grails-plugin grails-2.0

I am using grails 2.1.0. I have installed spring-security-core plugin.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.