Android_5.0定制--------在Launcher界面改变SystemUI中的NavigationBar和StatusBar背景颜色

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

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值