java - Why margin top and layout_above in relative view behave this way? -


i trying place view on top of view , bit outside of bounding box.
code simplified show problem:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"               android:layout_width="match_parent"               android:layout_height="match_parent">       <view         android:id="@+id/view"         android:layout_width="56dp"         android:layout_height="56dp"         android:layout_above="@+id/linear"         android:layout_margintop="200dp"         android:background="@color/red"         />      <linearlayout         android:id="@+id/linear"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_margintop="200dp"         android:orientation="vertical"         android:padding="0dp"         android:background="@color/white"         >          <view             android:layout_width="match_parent"             android:layout_height="200dp"             android:background="@color/orange"             />     </linearlayout>     </relativelayout> 

the result:

enter image description here

the second arrow shows expected small rectangle view.
why show on top although have specified same top margin linear layout bellow it?
if remove android:layout_above="@+id/linear" goes second arrow shows either bellow orange view , not above it.
why relative layout that?

it not relativelayout nature of margins. if put view (orange box) , there margin of 200dp above it, no other view can placed in 200dp margin.

to center orange box , put view above need this.

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent">       <view         android:id="@+id/view"         android:layout_width="56dp"         android:layout_height="56dp"         android:layout_above="@+id/center_view"         android:background="@color/red" />       <view         android:id="@+id/center_view"         android:layout_width="match_parent"         android:layout_height="200dp"         android:layout_centerinparent="true"         android:background="@color/orange" />   </relativelayout> 

this put orange view in center , red view directly on top of it. notice don't need linearlayout can have orange view in relativelayout directly.


Comments