FitsSystemWindows removes registration

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 in my application there is no paddingLeft or paddingRight . When I fitsSystemWindows , the indentation returns. What for? How can I save fitsSystemWindows and add-ons?

+5
source share
2 answers

fitsSyatemWindows attribute overrides the addition of the applies layout.

So, to apply the add-on, you must create one shell layout for your RelativeLayout and add the fitsSystemWindows attribute and padding for the 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> 
+4
source

I will just add this here if someone needs to remove the top add-on when using fitSystemWindows. This can occur when using custom action bars, DrawerLayout / NavigationView and / or fragments.

 public class CustomFrameLayout extends FrameLayout { public CustomFrameLayout(Context context) { super(context); } public CustomFrameLayout(Context context, AttributeSet attrs) { super(context, attrs); } public CustomFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public CustomFrameLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override protected boolean fitSystemWindows(Rect insets) { // this is added so we can "consume" the padding which is added because // `android:fitsSystemWindows="true"` was added to the XML tag of View. if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN && Build.VERSION.SDK_INT < 20) { insets.top = 0; // remove height of NavBar so that it does add padding at bottom. insets.bottom -= heightOfNavigationBar; } return super.fitSystemWindows(insets); } @Override public WindowInsets onApplyWindowInsets(WindowInsets insets) { // executed by API >= 20. // removes the empty padding at the bottom which equals that of the height of NavBar. setPadding(0, 0, 0, insets.getSystemWindowInsetBottom() - heightOfNavigationBar); return insets.consumeSystemWindowInsets(); } } 

We need to extend the Layout class (FrameLayout in my case) and remove the top padding in fitSystemWindows() (for API <20) or onApplyWindowInsets() (for API> = 20).

+4
source

Source: https://habr.com/ru/post/1233751/


All Articles