由沉浸式状态栏引发的血案

原文链接:http://www.jianshu.com/p/140be70b84cd?utm_source=tuicool&utm_medium=referral

概述

在一个人云亦云的时代,在技术迅猛发展的时代,我们来不及去好好学习,好好理解,就这样和着别人说话,都没有好好思考过。由此引发了这一血案。

是透明不是沉浸

虽然说不大懂英文也是可以撸代码的,但是我总觉得此非长久之计也。英文还是得多学,免得阻碍了发展道路。由于对英文不感冒,所以才有了将Translucent Bar(透明栏)和Immersive Mode(沉浸模式)混为一谈的说法。下面是引自Android开发者官网的一段说法

Immersive full-screen mode

To provide your app with a layout that fills the entire screen, the new SYSTEM_UI_FLAG_IMMERSIVE flag for setSystemUiVisibility()(when combined with SYSTEM_UI_FLAG_HIDE_NAVIGATION enables a new immersivefull-screen mode. While immersive full-screen mode is enabled, your activity continues to receive all touch events. The user can reveal the system bars with an inward swipe along the region where the system bars normally appear. This clears the SYSTEM_UI_FLAG_HIDE_NAVIGATION flag (and the SYSTEM_UI_FLAG_FULLSCREEN flag, if applied) so the system bars remain visible. However, if you'd like the system bars to hide again after a few moments, you can instead use the SYSTEM_UI_FLAG_IMMERSIVE_STICKY flag.

Translucent system bars

You can now make the system bars partially translucent with new themes, Theme.Holo.NoActionBar.TranslucentDecor and Theme.Holo.Light.NoActionBar.TranslucentDecor.By enabling translucent system bars, your layout will fill the area behind the system bars, so you must also enable fitsSystemWindows for the portion of your layout that should not be covered by the system bars.
If you're creating a custom theme, set one of these themes as the parent theme or include the windowTranslucentNavigation and windowTranslucentStatus style properties in your theme.

简单翻译一下:

  1. 沉浸式全屏模式
    隐藏status bar(状态栏)使屏幕全屏,让Activity接收所有的(整个屏幕的)触摸事件。
  2. 透明化系统栏
    透明化系统栏,使得布局侵入系统栏的后面,必须启用fitsSystemWindows属性来调整布局才不至于被系统栏覆盖。

透明栏和沉浸模式的区别

如何在4.4以上中实现Translucent Bar

为什么要强调4.4呢?其一是Translucent Bar是4.4开始有的特性;其二是5.0开始,Google推出Material Design,使用Theme.Material(MD主题)或Theme.AppCompat主题并至少设置ColorPrimaryDark属性就可以实现status bar半透明效果。


Material&AppCompat的一些属性
跟着官方走

其实在上面,官方的说法已经很清楚了

第一步. 使用主题:
Theme.Holo.NoActionBar.TranslucentDecorandTheme.Holo.Light.NoActionBar.TranslucentDecor
点击上面的主题链接看一下,原来只是设置了属性 windowTranslucentStatuswindowTranslucentNavigation 为true,我们可以不用官方说的主题,但是必须设置那两个属性的值为true才行,以便使我们定义的layout侵入系统栏的领域。说到这里,我们有必要聊聊什么事系统栏(system bar)。看下面的图,系统栏包含了顶部状态栏和底部导航栏。


各种bar

第二步. 设置 fitsSystemWindows 属性为true来进行布局调整

官方描述:
Boolean internal attribute to adjust view layout based on system windows such as the status bar. If true, adjusts the padding of this view to leave space for the system windows. Will only take effect if this view is in a non-embedded activity.

简单翻译下,就是哪个view设置了这个属性为true,系统就会调整该view的padding值来留出空间给系统窗体。表现为,padding出status bar的高度给status bar使用,不至于我们定义layout跟status bar重叠!
实际的使用可以参考Clock哥的简书:Translucent System Bar 的最佳实践,在此就不赘述了。


值的一提的是:该属性最好是用在我们需要进行调整的view中,而不是在theme中设置,因为theme设置的是整个窗体的属性,会影响使用了该theme的activity或application的行为,造成依附于Application Window的Window(比如Toast)错位。还有当Activity的ContentView中嵌套的layout有好几处使用了该属性时,会发生奇怪的错位问题。记住:只在需要padding出空间给system bar的layout添加该属性!


Toast错位的解决方法如下:使用应用级别的上下文

Toast.makeText(getApplicationContext(),"toast sth...",Toast.LENGTH_SHORT).show();
跟着民间大神走

第一步. 在res/values/文件夹中添加style-v19.xml,开启透明栏

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
    </style>
</resources>

第二步. 在基类中完成布局调整的工作

//摘自:http://www.jianshu.com/p/0acc12c29c1b
public abstract class TranslucentBarBaseActivity extends AppCompatActivity {

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

        setContentView(getLayoutResId());//把设置布局文件的操作交给继承的子类

        ViewGroup contentFrameLayout = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);
        View parentView = contentFrameLayout.getChildAt(0);
        if (parentView != null && Build.VERSION.SDK_INT >= 14) {
            parentView.setFitsSystemWindows(true);
        }
    }

    /**
     * 返回当前Activity布局文件的id
     *
     * @return
     */
    abstract protected int getLayoutResId();
}

简述下他的思路:试图将设置内容布局的操作交给子类,而基类获取内容布局的根布局,判断是否为4.4或以上的运行环境,再设置fitsSystemWindows属性为true,达到调整内容布局的目的。

自己动手,丰衣足食
  1. 启用透明化:同上,不赘述
  2. 自己fitsSystemWindows,不用系统帮我
public class BaseActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT){
            ViewGroup firstChildAtDecorView = ((ViewGroup) ((ViewGroup)getWindow().getDecorView()).getChildAt(0));
            View statusView = new View(this);
            ViewGroup.LayoutParams statusViewLp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    getStatusBarHeight());
            //颜色的设置可抽取出来让子类实现之
            statusView.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light));
            firstChildAtDecorView.addView(statusView, 0, statusViewLp);
        }
    }
    private int getStatusBarHeight() {
        int resId = getResources().getIdentifier("status_bar_height","dimen","android");
        if(resId>0){
            return getResources().getDimensionPixelSize(resId);
        }
        return 0;
    }
}

效果图如下,会有一条莫名的黑线,暂时没有找到解决方案,知道的童鞋不妨交流一下。


效果图

简述下我的思路:
Activity持有一个PhoneWindow,PhoneWindow持有一个根View,叫DecorView(是一个FrameLayout),DecorView持有一个LinearLayout,在LinearLayout下分配两个FrameLayout,一个给ActionBar(当设置主题为NoActionBar是为ViewStub),一个给ContentView。不管如何,只要我们在LinearLayout的第一个位置插入一个View就可以让ContentView下移了!!


实现原理

更新日志 ↓ ↓ ↓


3月3日更新:

  • 优化文档结构,方便阅读
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值