美文网首页
Android沉浸式状态栏的实现

Android沉浸式状态栏的实现

作者: 资本家大恶人 | 来源:发表于2020-05-02 18:49 被阅读0次

在MainActivity中的onCreate方法

requestWindowFeature(Window.FEATURE_NO_TITLE);
        Window window = getWindow();
        window.getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
   window.setStatusBarColor(Color.parseColor("#00000000"));//根据顶部控件该颜色
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }

        ViewGroup contentFrameLayout = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);
        View parentView = contentFrameLayout.getChildAt(0);
        if (parentView != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            parentView.setFitsSystemWindows(true);
        }
     
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorWrite</item>
    <item name="colorPrimaryDark">@color/colorWrite</item>
    <item name="colorAccent">@color/colorWrite</item>
    <item name="android:windowIsTranslucent">true</item>
</style>

透明状态栏使用方法

1.在res文件夹下创建style-V21文件
2.复制style文件

<item name="android:statusBarColor">#00ffffff</item>

相关文章

网友评论

      本文标题:Android沉浸式状态栏的实现

      本文链接:https://www.haomeiwen.com/subject/rtqlghtx.html