Android : Different behaviour of "Match_parent" in LinearLayout and RelativeLayout - Android

on Sunday, October 26, 2014


Similar to this question A MATCH_PARENT view inside a WRAP_CONTENT view


Generally I understand the answer in the above question. However I can not apply the answer to my contrived example(below) and the layout behaviour seems to be different in Linear and RelativeLayout container.


Firstly I use a LinearLayout as container View A, and a button as child view B, The View A is set to be "wrap_content" and View B being "march_parent". the result is a small button at top left.



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout <!-- container View A -->

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FFFFFF">


<Button <!-- child View B -->
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="test"
/>

</LinearLayout>


enter image description here


Then I tried a RelativeLayout as container View A, I found the button take the whole window. So my question is that , with all the layout properties being same, why there is such a difference?



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout <!-- container View A changed to RelativeLayout -->

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FFFFFF">


<Button <!-- child View B -->
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="test"
/>

</RelativeLayout>


enter image description here


0 comments:

Post a Comment