Search code examples
androidoverlayandroid-relativelayout

ChildView in the last of RelativeLayout cannot see when API>20


Here is my code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
>
<Button
    android:text="button"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    />
<TextView
    android:text="right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
     />
</RelativeLayout>

when API>20,TextView was cover by the button,how to fix it?if the first child is a TextView,it does not happen.Does it a bug in android?


Solution

  • You have root layout match_parent in height and width you are occupying complete space by button with android:layout_height=match_parent

    and for textview you are not using reference of button to place over the layout Relative layout provides android:layout_below attribute you can use this for placing your textview below button

    check below example will give you more details

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <Button
            android:text="button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button"
            android:background="#ffffff"
            />
        <TextView
            android:text="right"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/button"
            android:layout_alignParentRight="true"
            />
    </RelativeLayout>
    

    To Highlight : This is not bug in android you are not using it in proper way