替换片段时,Android fitsSystemWindows无法正常工作 [英] Android fitsSystemWindows not working when replacing fragments
问题描述
我有SingleFramgnetActivity
,其目的只是保留和替换其中的片段.
I have SingleFramgnetActivity
whose purpose is only to hold and replace fragments inside it.
布局看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".SingleFragmentActivity"
>
<include layout="@layout/toolbar"/>
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
我要替换FrameLayout中的Fragments
.当我在Fragment
布局上将fitsSystemWindows
设置为true时,它没有响应.实际上,它仅在创建Activity
时有效,但是一旦我替换了FrameLayout
内的Fragment
,则fitsSystemWindows
参数将被忽略,并且布局位于状态栏和导航栏下方.
I'm replacing the Fragments
inside the FrameLayout. When I set the fitsSystemWindows
to true on the Fragment
layout, it is not responding. Actually it is working only when Activity
is created, but once I replace the Fragment
inside the FrameLayout
, the fitsSystemWindows
parameter is ignored and the layout is below the status bar and navigation bar.
我找到了一些解决方案,其中包含自定义FrameLayout使用不推荐使用的方法,但是由于某种原因,它对我不起作用(与普通FrameLayout的结果相同),而且我也不喜欢使用不推荐使用的方法的想法.
I found some solution with custom FrameLayout which is using deprecated methods, but for some reason it is not working for me (same result as with normal FrameLayout) and I also do not like the idea to use deprecated methods.
推荐答案
您的FrameLayout
不知道窗口插入大小,因为它是父级-LinearLayout
尚未分派他.解决方法是,您可以将LinearLayout
子类化,然后将插图自行传递给子代:
Your FrameLayout
is not aware of window inset sizes, because it's parent - LinearLayout
hasn't dispatched him any. As a workaround, you can subclass LinearLayout
and pass insets to children on your own:
@TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
@Override
public WindowInsets onApplyWindowInsets(WindowInsets insets) {
int childCount = getChildCount();
for (int index = 0; index < childCount; index++)
getChildAt(index).dispatchApplyWindowInsets(insets); // let children know about WindowInsets
return insets;
}
您可以查看我的答案,它将详细说明其工作原理以及使用方法 ViewCompat.setOnApplyWindowInsetsListener
API.
You can have a look to my this answer, which will explain detailed how this works, and also how to use ViewCompat.setOnApplyWindowInsetsListener
API.
这篇关于替换片段时,Android fitsSystemWindows无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!