改变Android状态栏的颜色
2015-10-08 11:20
597 查看
Android 5.0 Lollipop introduced Material Design theme which automatically colors the status bar based on the ColorPrimaryDark value of the theme.this
requires min API 21
the way to do this without any Library: if the OS version is not supported - under kitkat - so nothing happend.
added to the top this View:
2.add this method:
3. add method get action Bar & status bar height:
4.then the only thing you need is this line to set status bar color:
requires min API 21
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light"> <!-- colorPrimary is used for the default action bar background --> <item name="colorPrimary">@color/my_awesome_color</item> <!-- colorPrimaryDark is used for the status bar --> <item name="colorPrimaryDark">@color/my_awesome_darker_color</item> <!-- colorAccent is used as the default value for colorControlActivated which is used to tint widgets --> <item name="colorAccent">@color/accent</item> <!-- You can also set colorControlNormal, colorControlActivated colorControlHighlight & colorSwitchThumbNormal. --> </style>
the way to do this without any Library: if the OS version is not supported - under kitkat - so nothing happend.
added to the top this View:
<View android:id="@+id/statusBarBackground" android:layout_width="match_parent" android:layout_height="wrap_content" />
2.add this method:
public void setStatusBarColor(View statusBar,int color){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window w = getWindow(); w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //status bar height int actionBarHeight = getActionBarHeight(); int statusBarHeight = getStatusBarHeight(); //action bar height statusBar.getLayoutParams().height = actionBarHeight + statusBarHeight; statusBar.setBackgroundColor(color); } }
3. add method get action Bar & status bar height:
public int getActionBarHeight() { int actionBarHeight = 0; TypedValue tv = new TypedValue(); if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) { actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics()); } return actionBarHeight; } public int getStatusBarHeight() { int result = 0; int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { result = getResources().getDimensionPixelSize(resourceId); } return result; }
4.then the only thing you need is this line to set status bar color:
setStatusBarColor(findViewById(R.id.statusBarBackground),getResources().getColor(android.R.color.white));[/code]
相关文章推荐
- Android EventBus发布/订阅事件总线
- android项目文件介绍
- android 常见分辨率(mdpi、hdpi 、xhdpi、xxhdpi )及屏幕适配注意事项
- Android之使用Android-query框架开发实战(二)
- Android 中 View 炸裂特效的实现分析
- android开发注意事项
- Android实战简易教程-第六十三枪(动画实现唱片播放界面)
- android 电容屏(三):驱动调试之驱动程序分析篇
- android 电容屏(二):驱动调试之基本概念篇
- Android之使用Android-query框架开发实战(一)
- android 电容屏(一):电容屏基本原理篇
- Android中解析lrc歌词 同步歌曲
- Android项目中的双击退出应用
- Android代码规范
- Android - Event Handling
- android开发
- Android应用开发之所有动画使用详解
- 目前调试移动设备程序只能通过USB线缆
- android-工程目录结构
- Android Intent Standard Actions