【Android UI】状态栏和toolbar颜色一致
2015-07-30 11:18
417 查看
1、在style.xml中定义toolbar的颜色
2、布局文件添加属性
3、在Activity的onCreate(Bundle savedInstanceState)方法中添加代码
<resources> <!-- Base application theme. --> <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <!-- toolbar(actionbar)颜色 --> <item name="colorPrimary">#4876FF</item> <item name="android:windowTranslucentStatus">true</item> </style> <style name="AppTheme" parent="@style/AppBaseTheme"></style> </resources>
2、布局文件添加属性
android:fitsSystemWindows="true" android:clipToPadding="true"
3、在Activity的onCreate(Bundle savedInstanceState)方法中添加代码
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window window = getWindow(); window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); //获取样式中的属性值 TypedValue typedValue = new TypedValue(); this.getTheme().resolveAttribute(android.R.attr.colorPrimary, typedValue, true); int[] attribute = new int[] { android.R.attr.colorPrimary }; TypedArray array = this.obtainStyledAttributes(typedValue.resourceId, attribute); int color = array.getColor(0, Color.TRANSPARENT); array.recycle(); window.setStatusBarColor(color); } setContentView(R.layout.activity_main); }
相关文章推荐
- 62. Unique Paths
- 队列(Queue) C 语言实现
- HDOJ 5147 Sequence II 树阵
- 格而知之2:UIView的autoresizingMask属性探究
- UILabel常用属性小结
- mui.fire()用法
- iOS UITableView使用详解
- UITableView介绍
- 安卓主界面UI不能实现下载
- UIViewController的presentedViewController,presentingViewController和parentViewController三个属性
- Equivalent Strings (字符串相等?)
- poj 2778 AC自动机+快速幂(DNA Sequence)
- 多线程入门教程四:MFC的多线程
- iOS开发UI篇—常见的项目文件介绍
- FreeBSD Kqueue的实现原理
- iOS UILabel根据字符串长度自动适应宽度和高度
- requestLayout, invalidate和postInvalidate的异同
- Android Stuido (AS)中应该忽略的那些文件
- OC中使用UI自定义控件实现计算器的设计(版本1简单的加减乘除,连加,连减,连除,连乘)
- 设置按钮(UIButton)为圆角矩形