user5447353 user5447353 - 5 months ago 62
Android Question

fitsSystemWindows removes padding

I have this in my layout:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/primary"
android:paddingLeft="32dp"
android:paddingRight="32dp"
android:fitsSystemWindows="true">

...

</RelativeLayout>


But there is no
paddingLeft
or
paddingRight
in my app. When I remove
fitsSystemWindows
, the padding comes back. Why? How can I keep
fitsSystemWindows
and the padding?

Answer

fitsSyatemWindows attribute overrides the padding applied the layout.

So to apply the padding, you should create one wrapper layout to your RelativeLayout and add fitsSystemWindows attribute to it, and padding to child RelativeLayout.

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/primary"
    android:fitsSystemWindows="true">        //this is container layout

    <RelativeLayout
         android:paddingLeft="32dp"
         android:paddingRight="32dp"
         ..... >                            //now you can add padding to this

          .....

    </RelativeLayout>
</RelativeLayout>