将子视图设置为适应系统窗口大小

4
我正在设置一个简单的视图,其中只包含一个空的RelativeLayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:clipToPadding="false"
    android:background="@color/red"/>

使用这个主题:

<style name="FullscreenTheme" parent="BaseTheme">
    <item name="android:textColorSecondary">@android:color/white</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:navigationBarColor">@android:color/transparent</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
</style>

在onCreate方法中添加以下内容:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    getWindow().getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}

这将产生预期的效果:

enter image description here

然而,当我尝试添加一个子元素时,我无法做到同样的事情。XML看起来像这样:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:clipToPadding="false"
    android:background="@color/red">

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/bit_teal"
        android:fitsSystemWindows="true"
        android:clipToPadding="false"/>

</RelativeLayout>

以下是展示效果:

enter image description here

有什么想法吗?

1个回答

1
RelativeLayout中去掉android:fitsSystemWindows="true"。我认为两者不能同时适应系统视图,而父元素始终具有优先权。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接