Android_5.0定制--------在Launcher界面改变SystemUI中的NavigationBar和StatusBar背景颜色
2016-09-19 23:45
393 查看
SystemUI中的状态栏和导航栏在源码中的布局分别是:
状态栏:super_status_bar.xml,源码路径:\frameworks\base\packages\SystemUI\res\layout\super_status_bar.xml
导航栏:navigation_bar.xml,源码路径:\frameworks\base\packages\SystemUI\res\layout\navigation_bar.xml
StatusBar是怎样添加到屏幕上去的(NavigationBar也差不多,就记录一个StatusBar):
\frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\phone\PhoneStatusBar.java中有一个StatusBarWindowView对象和StatusBarWindowManager对象。 其中两个对象的主要作用分别是:
StatusBarWindowView:通过View的inflate的静态方法将super_status_bar.xml实例化StatusBarWindowView的一个实例。(解析布文件)
StatusBarWindowManager:StatusBarWindowManager中存在一个WindowManager,先通过StatusBarWindowManager的add方法将StatusBarWindowView传递给WindowManager,然后WindowManager通过addView方法将View添加到窗口上。(将View添加到窗口,也就是屏幕上)
上面就是大致StatusBar的添加到屏幕的过程。
下面说一下怎么设置StatusBar背景颜色:
我们是要在Launcher界面修改StatusBar背景颜色,Launcher也继承自Activity。一般情况下,Android系统给出了接口Window类里面有setNavigationBarColor和setStatusBarColor两个方法去设置状态栏和导航栏的背景颜色。
状态栏:super_status_bar.xml,源码路径:\frameworks\base\packages\SystemUI\res\layout\super_status_bar.xml
导航栏:navigation_bar.xml,源码路径:\frameworks\base\packages\SystemUI\res\layout\navigation_bar.xml
StatusBar是怎样添加到屏幕上去的(NavigationBar也差不多,就记录一个StatusBar):
\frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\phone\PhoneStatusBar.java中有一个StatusBarWindowView对象和StatusBarWindowManager对象。 其中两个对象的主要作用分别是:
StatusBarWindowView:通过View的inflate的静态方法将super_status_bar.xml实例化StatusBarWindowView的一个实例。(解析布文件)
StatusBarWindowManager:StatusBarWindowManager中存在一个WindowManager,先通过StatusBarWindowManager的add方法将StatusBarWindowView传递给WindowManager,然后WindowManager通过addView方法将View添加到窗口上。(将View添加到窗口,也就是屏幕上)
上面就是大致StatusBar的添加到屏幕的过程。
下面说一下怎么设置StatusBar背景颜色:
我们是要在Launcher界面修改StatusBar背景颜色,Launcher也继承自Activity。一般情况下,Android系统给出了接口Window类里面有setNavigationBarColor和setStatusBarColor两个方法去设置状态栏和导航栏的背景颜色。
Launcher的主题<style name="Theme" parent="@android:style/Theme.Holo.Wallpaper.NoTitleBar">,在Theme主题下添加<item
name="android:windowDrawsSystemBarBackgrounds">true</item>属性。
在Launcher的onCreate方法中调用:
try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Window window = activity.getWindow(); window.setStatusBarColor(Color.parseColor("#ff000000")); } } catch (Exception e) { e.printStackTrace(); }发现编完成后将 apk Push到系统,发现状态栏背景颜色没有变成我们预期的颜色。跟踪源码发现,在Launcher.java类中有这么一段代码的调用:
private void setupTransparentSystemBarsForLmp() { // TODO(sansid): use the APIs directly when compiling against L sdk. // Currently we use reflection to access the flags and the API to set the transparency // on the System bars. if (Utilities.isLmpOrAbove()) { try { getWindow().getAttributes().systemUiVisibility |= (View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); Field drawsSysBackgroundsField = WindowManager.LayoutParams.class.getField( "FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS"); getWindow().addFlags(drawsSysBackgroundsField.getInt(null)); Method setStatusBarColorMethod = Window.class.getDeclaredMethod("setStatusBarColor", int.class); Method setNavigationBarColorMethod = Window.class.getDeclaredMethod("setNavigationBarColor", int.class); setStatusBarColorMethod.invoke(getWindow(), Color.TRANSPARENT); setNavigationBarColorMethod.invoke(getWindow(), Color.TRANSPARENT); } catch (NoSuchFieldException e) { Log.w(TAG, "NoSuchFieldException while setting up transparent bars"); } catch (NoSuchMethodException ex) { Log.w(TAG, "NoSuchMethodException while setting up transparent bars"); } catch (IllegalAccessException e) { Log.w(TAG, "IllegalAccessException while setting up transparent bars"); } catch (IllegalArgumentException e) { Log.w(TAG, "IllegalArgumentException while setting up transparent bars"); } catch (InvocationTargetException e) { Log.w(TAG, "InvocationTargetException while setting up transparent bars"); } finally {} } }这段代码就是系统利用反射机制调用Window中的setStatusBarColor方法,将状态栏的背景设置成透明。可以在该方法中做适当的处理,让前面设置的背景颜色就起作用了。
相关文章推荐
- android颜色关于Android TabHost切换Tab字体的颜色背景颜色改变
- 如何通过代码动态改变android的窗口背景颜色
- android状态栏一体化(改变状态栏的背景颜色)开源工程推荐
- Android—改变LinearLayout背景颜色
- 改变Android的hello world程序字体颜色和背景颜色
- 【Android 界面效果20】Android GradientDrawable类的详解,设置activity的背景颜色渐变效果
- ios 7 statusbar 状态栏 跟随navigationbar的颜色而改变
- Android LinearLayout 点击背景颜色改变
- android点击Button后,buttton的背景和颜色改变
- android背景颜色渐变及改变title颜色
- android背景颜色渐变及改变title颜色
- 利用Android的传感器改变背景颜色
- android gridview点击子选项动态改变item背景颜色
- ios 7 statusbar 状态栏 跟随navigationbar的颜色而改变
- 关于Android TabHost切换Tab字体的颜色背景颜色改变
- android 界面 按button或者imagebutton改变图片或者背景的效果
- android 改变 activity 背景颜色
- android状态栏一体化(改变状态栏的背景颜色)开源工程推荐
- Android如何让TextView子元素随着父元素的背景变化而改变字体颜色?
- Android下ListView上下滑动背景颜色改变