策略
安卓的系统栏包括了顶部状态栏和底部导航栏,少数部分机型如nexus、华为荣耀6等在界面视图中存在可操作的底部导航栏。综合考虑安卓碎片化适配问题,遵循市场app主流设计风格,对系统栏做一下设计处理
- Activity页面遵循状态栏着色,导航栏按需处理。状态栏可选择透明处理或着色处理,导航栏处理的页面常见于闪屏页、引导页或者视频观看页等;
- 统一在styles样式中设置theme,只处理4.4版本以上机型。截止2016/09/05,安卓官网数据表明4.4及以上版本机型占所有安卓手机比例为81.4,不必承担不可控的低版本适配风险;
- 统一在xml或者代码设置是否fitsSystemWindows。具体风格由个人而定;
- 统一在应用Acitivity基类中决定透明栏着色。子类默认着色选择colorPrimary,可根据业务自定覆盖选择其他颜色。
实现
styles样式定义
1 | <!--Base application theme. 4.4 以下版本 --> |
注意:上述设置不对导航栏进行处理(theme遵循普遍性),如需要特殊处理再针对特定Activity写theme即可。5.0以上版本设置和4.4版本不同,原因在于按照4.4版本的设置windowTranslucentStatus,实际机型看到的效果为半透明,需要多设置一个透明值。
设置fitSystemWindows
1 | /** |
状态栏着色
1 | /** |
实践
下面是项目中实际处理系统栏的通用做法,统一在Activity处理。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26public class BaseActivity extends AppCompatActivity {
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SystemUiUtil.initSystemBar(this, getSystemBarColor());
}
protected void onStart() {
super.onStart();
if(isFitSystemWindows()){
SystemUiUtil.setFitsSystemWindows(this);
}
}
/** 子类可继承实现染色 **/
public int getSystemBarColor(){
return ContextCompat.getColor(this,R.color.colorPrimary);
}
/** 子类可继承决定是否适应 **/
public boolean isFitSystemWindows(){
return true;
}
}
其中initSystemBar方法和setFitsSystemWindows方法封装于SystemUiUtil类中,具体实现见仁见智。
具体代码可参考AppArchitecture项目。