Android一体式(沉浸式)状态栏的实现
来源:互联网 发布:linux常用命令有什么用 编辑:程序博客网 时间:2024/04/29 21:10
注:公司开发任务适配是在4.4版本之上进行,所以此适配仅在4.4之上进行测试。
1、主要使用了第三方的开源项目SystemBarTint,github:https://github.com/jgilfelt/SystemBarTint
2、根据SystemBarTint自带sample进行研究,主要步骤如下:
1、在Activity中加入如下代码:
public class MainActivity extends BaseActivity { @Override public void setContentView() { setContentView(R.layout.activity_main); // 沉浸式状态栏 setTranslucentStatus(); } } /** * 为xml 的根布局添加android:fitsSystemWindows=”true” 属性<br/> */ protected void setTranslucentStatus() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { setTranslucentStatus(true); } SystemBarTintManager tintManager = new SystemBarTintManager(this); tintManager.setStatusBarTintEnabled(true); tintManager.setStatusBarTintResource(R.color.statusbar_bg);// 通知栏所需颜色 } @TargetApi(19) private void setTranslucentStatus(boolean on) { Window win = getWindow(); WindowManager.LayoutParams winParams = win.getAttributes(); final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; if (on) { winParams.flags |= bits; } else { winParams.flags &= ~bits; } win.setAttributes(winParams); }
注:有些资料说setTranslucentStatus()方法需要放在setConentView()之后,实际上没有影响。
2、在MainActivity的根布局中添加:
android:fitsSystemWindows="true"
3、如果我们在布局中间加入一个自定义TextView控件,并且设置的背景色和setTranslucentStatus()方法中的color一样的话,我们运行会发现效果如下图:
其中,在statusbar与textview中间会有一个空白,这个空白是actionbar的留白,所以,如果使用自带titlebar的话,要取消actionbar的显示;如果使用actionbar的话,要将actionbar的背景设置为相同色。
public class MainActivity { @Override public void setContentView() { noTitleBar(); setContentView(R.layout.activity_main); // 沉浸式状态栏 setTranslucentStatus(); } /** * 不显示标题栏 */ protected void noTitleBar() { this.requestWindowFeature(Window.FEATURE_NO_TITLE); }}
效果如下:
4、由于github上面的代码是基于gradle的,下面放出基于eclipse的版本源码。
0 0
- Android一体式(沉浸式)状态栏的实现
- Android 沉浸式状态栏 实现方式一
- Android 沉浸式状态栏实现攻略(一)
- Android沉浸式状态栏(一)
- android沉浸式状态栏--解决方案(一)
- Android沉浸式状态栏(一)
- android沉浸式状态栏的实现
- Android沉浸式状态栏的实现
- android沉浸式状态栏的实现
- Android 状态栏沉浸式的实现
- Android 沉浸式状态栏的实现
- Android 4.4 沉浸式状态栏的实现
- android 沉浸式状态栏的实现
- Android 沉浸式状态栏的实现
- Android沉浸式状态栏的实现
- Android沉浸式状态栏的初步实现
- Android沉浸式状态栏的简单实现
- android沉浸式状态栏的实现
- activeMQ安装配置
- Oracle PL/SQL入门语法点
- Git安装配置
- 顺序表应用2:多余元素删除之建表算法
- Java 验证码实践
- Android一体式(沉浸式)状态栏的实现
- C++中栈和队列
- 推荐系统总结
- 15章上机练习4
- hdu 1247 Trie入门题
- Android实现顶部轮播图
- Android6.0.1音乐apk获得audioFocus的流程
- 单点登陆_配置客户端与服务器端
- thinkphp中memcache缓存