Search code examples
androidxmlandroid-layoutandroid-studioandroid-relativelayout

Empty view appears in unexpected place in Android


I have this layout :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:orientation="horizontal"
        android:layout_centerInParent="true"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/mapbox_fill_white"/>



    </LinearLayout>

    <View
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="100dp">
    </View>


</RelativeLayout>

When I add the below view, it appears above the linear layout, but I want it to be below layout. Why is it going above the layout? How can I fix this? Thanks.


Solution

  • use attribute android:layout_below to show it below LinearLayout ,

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingBottom="@dimen/activity_vertical_margin"
                    android:paddingLeft="@dimen/activity_horizontal_margin"
                    android:paddingRight="@dimen/activity_horizontal_margin"
                    android:paddingTop="@dimen/activity_vertical_margin" >
    
        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerInParent="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="40dp"
            android:orientation="horizontal" >
    
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="asdf"
                android:background="@color/mapbox_fill_white" />
        </LinearLayout >
    
        <View
            android:layout_width="fill_parent"
            android:layout_height="100dp"
            android:layout_below="@+id/linearLayout"
            android:layout_weight="1" >
        </View >
    </RelativeLayout >
    

    android:layout_below -- Positions the top edge of this view below the given anchor view ID. Accommodates top margin of this view and bottom margin of anchor view.

    Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

    check RelativeLayout LayoutParams