如何具有透明的状态栏,但使导航栏不透明? [英] How to Have a Transparent Status Bar but Leave Navigation Bar Opaque?

查看:138
本文介绍了如何具有透明的状态栏,但使导航栏不透明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序上有一个透明的状态栏(这样背景就在它后面),但是我希望底部的导航栏保持黑色.

I want to have a transparent status bar on my app (so the background goes behind it) but I want the navigation bar at the bottom to stay black.

我可以通过设置 getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

我可以通过设置< item name ="android:windowTranslucentStatus"> true</item>

但是,如果不使底部也透明,就不能使顶部完全透明.使用< item name ="android:statusBarColor"> @android:color/transparent</item> 或类似方法无效.

However, I can't make the top completely transparent without making the bottom transparent too. Using <item name="android:statusBarColor">@android:color/transparent</item> or similar doesn't work.

有人知道如何制作完全透明的状态栏而不影响导航栏吗?

Does anyone know how to make a fully transparent status bar without affecting the navigation bar?

推荐答案

要独立控制KitKat上状态和导航栏的透明度,您只需使用窗口管理器标志 FLAG_TRANSLUSCENT_STATUS onCreate()方法中的> FLAG_TRANSLUSCENT_NAVIGATION .但是,在KitKat上,系统可以在状态栏上绘制可绘制的半不透明渐变稀松布.这似乎是特定于设备的:在我的KitKat设备上,它是完全透明的,但是在Android Studio模拟器上,它显示为稀松布.

To independently control the transluscency of the status and navigation bars on KitKat, you can simply use the window manager flags FLAG_TRANSLUSCENT_STATUS and FLAG_TRANSLUSCENT_NAVIGATION in the onCreate() method of your activity. However, on KitKat the system may draw a semi-opaque gradient scrim drawable over the status bar. This appears to be device-specific: on my KitKat device it's fully transparent, but on Android Studio emulator it shows a scrim.

在Lolliop或更高版本上,您可以使用 Window#setStatusBarColor(int)方法和 FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS 窗口管理器标记来设置状态栏颜色.请注意,在这种情况下,您清除 FLAG_TRANSLUSCENT_STATUS 标志.如果您希望颜色是透明的,则意味着您的应用程序支持全屏模式并设置系统UI可见性,因此,由您的应用程序来管理状态栏背景色.

On Lolliop or later you can instead set the status bar colour using the Window#setStatusBarColor(int) method together with the FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS window manager flag. Note that you clear the FLAG_TRANSLUSCENT_STATUS flag in this case. If you want the colour to be transparent, that implies that your application supports full screen mode and sets the system UI visibility, so it's down to your app to manage the status bar background colour.

将所有内容放在一起看起来像这样:

Putting it all together would look something like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Window window = getWindow();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(0x00000000);  // transparent
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        int flags = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        window.addFlags(flags);
    }
    setContentView(R.layout.main);
    window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}

使用的布局示例:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff00ff00"
    android:fitsSystemWindows="false"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Hello World!"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

    </LinearLayout>

</RelativeLayout>

示例屏幕截图.

棒棒糖:

KitKat:

这篇关于如何具有透明的状态栏,但使导航栏不透明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆