安卓:如何将状态栏和导航栏设置为半透明

4
在一些应用程序中,我注意到状态栏和导航栏是透明的,但不完全透明。我不知道这是否是背景颜色或其他什么东西。请问有人可以告诉我如何实现这个效果吗?谢谢。 enter image description here
3个回答

11
你可以使用这两种方法:

getWindow().setStatusBarColor(Color.parseColor("#20111111"));
getWindow().setNavigationBarColor(Color.parseColor("#20111111"));

谢谢,你知道 Google 使用的确切颜色吗? - Lord Goderick
1
我不知道确切的颜色,但Color.parseColor("#20111111")可以帮助你。 - ak sacha
谢谢,这非常接近。 - Lord Goderick
1
愉快编码 :) - ak sacha
7
我正在变得越来越不透明的颜色。 - Choletski

6

只设置导航栏颜色和状态栏颜色还不够,您需要确保内容实际上出现在它们的下方。

我在我的工具类中使用以下方法,在setContentView之前在活动中进行设置。

public static void setWindowStatusNav(android.view.Window window, int statusbarColor, int navbarColor) {

    int flags = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;

    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT || Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT_WATCH) {
        window.getAttributes().flags |= flags;
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        int uiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
        window.getDecorView().setSystemUiVisibility(uiVisibility);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        window.getAttributes().flags &= ~flags;

        window.setStatusBarColor(statusbarColor);
        window.setNavigationBarColor(navbarColor);
    }
}

在你的活动中使用它:

public class MyActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int statusBarColor = android.graphics.Color.parseColor("#40FF0000");
        int navBarColor = android.graphics.Color.parseColor("#6E00FF00");
        MyUtils.setWindowStatusNav(getWindow(), statusBarColor, navBarColor);

        setContentView(R.layout.my_activity);
    }
}

隐藏导航栏和状态栏,请调用此方法:
public static void setWindowStatusNavHidden(android.view.Window window) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        int uiVisibility = window.getDecorView().getSystemUiVisibility();
        uiVisibility |= View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

        window.getDecorView().setSystemUiVisibility(uiVisibility);
    }
}

使用完成:

public class MyActivity extends AppCompatActivity {

    static boolean statusNavVisible = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int statusBarColor = android.graphics.Color.parseColor("#40FF0000");
        int navBarColor = android.graphics.Color.parseColor("#6E00FF00");
        MyUtils.setWindowStatusNav(getWindow(), statusBarColor, navBarColor);

        setContentView(R.layout.my_activity);

        Button btnShowHide = findViewById(R.id.my_button);
        btnShowHide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (statusNavVisible) {
                    statusNavVisible = false;
                    MyUtils.setWindowStatusNavHidden(getWindow());
                } else {
                    statusNavVisible = true;
                    MyUtils.setWindowStatusNav(getWindow(), statusBarColor, navBarColor);
                }
            }
        });
    }
}

1
如果你只想让它半透明,可以在你的主题中添加这个代码。
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>

如果你也想将其隐藏并使你的活动全屏。

    <item name="android:windowActionBarOverlay">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>

并在您的 Activity 的 onCreate 方法中添加。

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    window.setFlags(
        WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN
    );
    if (Build.VERSION.SDK_INT >= 30) {
        findViewById<ConstraintLayout>(R.id.main_activity_parent).windowInsetsController?.hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
    } else {
        findViewById<ConstraintLayout>(R.id.main_activity_parent).systemUiVisibility =
            View.SYSTEM_UI_FLAG_LOW_PROFILE or
                    View.SYSTEM_UI_FLAG_FULLSCREEN or
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                    View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
    }

1
是的,至少它在API 31上有效,谢谢! - sarkiroka

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