沉浸式

作者: 7i昂 | 来源:发表于2019-10-04 09:56 被阅读0次

我对沉浸式的理解就是状态栏和导航栏变为透明
把背景图片和标题栏显示出来
比如


image.png

通过theme实现方式

//valuse
<style name="TranslucentTheme" parent="AppTheme">
</style>

// values-v19。v19 开始有 android:windowTranslucentStatus 这个属性
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
</style>

// values-v21。5.0 以上提供了 setStatusBarColor()  方法设置状态栏颜色。
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowTranslucentStatus">false</item>
    <item name="android:windowTranslucentNavigation">true</item>
    <!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

通过代码设置

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
    int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Window window = getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.flags |= flagTranslucentNavigation;
        window.setAttributes(attributes);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
    } else {
        Window window = getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.flags |= flagTranslucentStatus | flagTranslucentNavigation;
        window.setAttributes(attributes);
    }
}
/**
 * 通过设置全屏,设置状态栏透明
 *
 * @param activity
 */
private void fullScreen(Activity activity) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色
            Window window = activity.getWindow();
            View decorView = window.getDecorView();
            //两个 flag 要结合使用,表示让应用的主体内容占用系统状态栏的空间
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            //导航栏颜色也可以正常设置
//                window.setNavigationBarColor(Color.TRANSPARENT);
        } else {
            Window window = activity.getWindow();
            WindowManager.LayoutParams attributes = window.getAttributes();
            int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
            attributes.flags |= flagTranslucentStatus;
//                attributes.flags |= flagTranslucentNavigation;
            window.setAttributes(attributes);
        }
    }
}

图一发现标题栏被顶上去了解决方法有三种

法一:设置 fitsSystemWindows 属性
法二:布局里添加占位状态栏
法三:代码中设置 paddingTop 并添加占位状态栏

相关文章

  • 沉浸式学习

    怎么样才能沉浸式学习,为什么我只能沉浸式玩手机? 他们沉浸式回家,沉浸式学习,沉浸式生活,总之,都能够沉浸进去。而...

  • 适配Android刘海屏小结

    by hzwusibo 20180907 一、沉浸式与非沉浸式来回切换页面适配 二、沉浸式页面适配 (非沉浸式...

  • Android 沉浸式模式与常见状态栏和导航栏效果

    Android沉浸式模式 官方称沉浸式状态栏为沉浸式模式。 什么是沉浸式?沉浸式就是让人专注当前的(由设计者营造)...

  • 沉浸式阅读 沉浸式生活

    10.13陪女儿上课,突然感受到那种沉浸式快乐,完全投入,女儿配合,我也很开心。下午还发现一个青年作家,喜欢这种有...

  • 被打乱了的生活节奏

    沉浸式追剧,沉浸式护肤,沉浸式逛街,沉浸式购物,沉浸式工作……这些,都远我而去了 本学年第一次月考,请假,因为哺乳...

  • “沉浸式”体验

    最近注意到一个新词儿“沉浸式”,偶尔刷视频,总是能看到各种沉浸式标题的视频,“沉浸式看房”,“沉浸式做饭”,“沉浸...

  • 沉浸式

    http://www.cnblogs.com/android-blogs/p/5690725.htmlhttp:/...

  • 沉浸式

    我对沉浸式的理解就是状态栏和导航栏变为透明把背景图片和标题栏显示出来比如 通过theme实现方式 通过代码设置 图...

  • 沉浸式

    通过主题设置状态栏 通过代码设置 上述代码实现了沉浸式,但是控件会跑到状态栏上,解决方法1、 在布局文件中设置一...

  • 沉浸式

    最近几年社会提倡更多的是阅读,都腹中有书气质华,读书可以改变生活,改变气质,不管是央视的诗词大塞,还是个个公众号a...

网友评论

      本文标题:沉浸式

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