您的位置:首页 > 产品设计 > UI/UE

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两个方法去设置状态栏和导航栏的背景颜色。
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方法,将状态栏的背景设置成透明。可以在该方法中做适当的处理,让前面设置的背景颜色就起作用了。

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息