状态栏下方没有布局显示的半透明导航

4
我正在使用新的Android 4.4 FLAG_TRANSLUCENT_NAVIGATION 标志将屏幕底部的导航栏(返回,主页按钮等)变为半透明。这个方法很好用,但是其副作用是我的Activity布局现在显示在屏幕顶部状态栏的下面(即使我没有将状态栏设置为半透明)。我想避免采用hacky解决方案,比如在布局顶部应用填充。

有什么方法可以将导航栏设置为半透明,同时确保状态栏正常显示,并且不允许布局在其下面显示?

我正在使用以下代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    Window w = getWindow();
    w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}

谢谢

2个回答

6

ianhanniballake的回答实际上是有效的,但是你不应该在顶级视图上使用android:fitsSystemWindows="true"。请按照以下方式使用此属性:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/colorPrimaryDark">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#ffffff"
        android:fitsSystemWindows="true">

        <!-- Your other views -->

    </LinearLayout>
</LinearLayout>

如您所见,您需要在顶层视图上设置颜色,并将属性放置在另一个容器中。


1
android:fitsSystemWindows="true"添加到您的顶级视图 - android:fitsSystemWindows会自动调整视图大小,以考虑系统窗口,如状态栏。

7
这不起作用。它确实防止视图被定位在状态栏下方,但也防止视图出现在现在半透明的导航栏下方,这实际上是无用的。 - Milo

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