android 修改状态栏颜色
来源:互联网 发布:电脑远程软件 编辑:程序博客网 时间:2024/06/04 17:56
android状态栏颜色修改
android状态栏颜色修改
状态栏颜色的修改在4.4和5.x环境下分别有不同的方式,低于4.4以下是不能修改的。
5.x环境下
方式一,状态栏将显示为纯净的颜色,没有渐变效果
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
26
27
28
29
30
31
32
33
34
35
36
/**
* 状态栏相关工具类
*
*/
public
class
StatusBarUtils {
public
static
void
setWindowStatusBarColor(Activity activity,
int
colorResId) {
try
{
if
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(activity.getResources().getColor(colorResId));
//底部导航栏
//window.setNavigationBarColor(activity.getResources().getColor(colorResId));
}
}
catch
(Exception e) {
e.printStackTrace();
}
}
public
static
void
setWindowStatusBarColor(Dialog dialog,
int
colorResId) {
try
{
if
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = dialog.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(dialog.getContext().getResources().getColor(colorResId));
//底部导航栏
//window.setNavigationBarColor(activity.getResources().getColor(colorResId));
}
}
catch
(Exception e) {
e.printStackTrace();
}
}
}
效果图如下:状态栏被改成android.R.color.holo_blue_bright,标题栏颜色可以和状态栏一样,EditText的输入也没有受影响
ps:如果顶部为渐变效果,可能是在主题中设置
windowTranslucentStatus=true
属性。方式二:
通过Style来修改状态栏颜色。
1.设置 colorPrimary,colorPrimaryDark两个颜色。
1
2
3
4
<
stylename
="AppTheme"parent="Theme.AppCompat.Light.NoActionBar">
<
itemname
="colorPrimary">@android:color/holo_blue_bright</
item
>
<
itemname
="colorPrimaryDark">@android:color/holo_blue_bright</
item
>
</
style
>
2. AndroidManifest.xml文件中的targetSdkVersion必须设置在21以上。
3.parent主题必须是Theme.AppCompat开头,兼容包下的主题,所以必须一用v7包。
colorPrimary,colorPrimaryDark这两个属性是Material Design风格中规定的。具体位置如下图所示:
方式三:
1.在res/values-v19文件夹下添加styles.xml文件内容如下
1
2
3
<
stylename
="AppTheme"parent="@style/BaseAppTheme">
<
itemname
="android:windowTranslucentStatus">true</
item
>
</
style
>
2.顶部标题的控件设置两个属
android:background="@android:color/holo_blue_bright"
android:fitsSystemWindows="true"
则状态栏会保持与设置fitsSystemWindow属性的控件的背景颜色一致。
4.4环境下
上面的方式三也适用4.4环境。不过4.4和5.x下显示的效果有差异。根据本人测试结果来看,不同的手机厂商对于这种情况下,状态栏有的是渐变,有的是添加了一层黑色半透明层。
存在bug及解决办法
修改windowTranslucentStatus/Navigation="true"。 会导致EditText输入时,即使使用了 adjustResize,软键盘也会挡住 EditText
解决办法参考:
https://www.zhihu.com/question/31468556/answer/52136849?utm_source=weibo&utm_medium=weibo_share&utm_content=share_answer&utm_campaign=share_button
其他参考资料:
http://blog.csdn.net/lmj623565791/article/details/48649563
http://www.bkjia.com/Androidjc/971024.html
http://blog.sina.com.cn/s/blog_6e334dc70102ve7d.html
源代码:https://github.com/leon-HM/StatusBar
原文参考 : http://www.cnblogs.com/leon-hm/p/5131323.html
0 0
- Android状态栏颜色修改
- android状态栏颜色修改
- android 修改状态栏颜色
- android 状态栏颜色修改
- android状态栏颜色修改
- Android状态栏颜色修改
- android状态栏颜色修改
- android状态栏颜色修改
- android 修改状态栏颜色
- Android修改状态栏颜色
- Android状态栏颜色修改,取消状态栏
- Android实现修改状态栏颜色
- Android 状态栏的颜色修改
- android怎样修改状态栏颜色
- leon_hm android状态栏颜色修改
- android修改状态栏的颜色
- Android修改状态栏颜色详解
- Android状态栏颜色修改,activity状态栏颜色,非布局写法
- C++指针——(1)指针基础
- 启动Oracle数据库报错:systax error or unresolved network name“LISTENER_ORCL”
- 双目相机--双目视差与深度距离关系推导详解
- 【Python学习】之 简单图形编程
- 回顾2016展望2017
- android 修改状态栏颜色
- hdu 2504
- 【t042】炮击坦克
- Ubuntu下安装mariaDB 10.1 集群
- corejava_基础
- Java安全(四)-输入流的解密
- Ubuntu下安装多个Java及切换
- leetcode-441. Arranging Coins
- java实现双链表结构与基本数据操作