Android 4.4 实现透明状态栏Translucent system bar(Status bar)

Android 5.0的Material Design酷炫、吊炸天,先来解决黑黑的手机状态栏,再一步一步实现Material Design。
ThemeColors
对于Android 5.0 Status bar和App bar的背景色可以轻松定义,对于Android 4.4如何实现呢?
来两张效果图,那张更清晰、好看?
android_screenshot_default_system_bar_mark《—–》android_screenshot_translucent

Status bar

On Android, the status bar contains notification icons and system icons.
Android status bar height: 24dp(mi4 手机不是这个值-_-#)

ps:Android navigation bar 会引发一些问题,暂时不写了。

预备基础知识(Translucent system bars)

android:fitsSystemWindows

布局时是否考虑给系统窗口留出空间,true为留出空间(如 Status Bar and App bar)

android:clipToPadding

是否允许ViewGroup在padding中绘制,该值默认为true,即允许,padding无效;false:padding生效.

透明系统栏设置(Status bar 和 Navigation bar)
Theme.Holo.NoActionBar.TranslucentDecor
Theme.Holo.Light.NoActionBar.TranslucentDecor

如果设置了透明系统栏就必须设置fitsSystemWindows:true和android:clipToPadding:false(适用于listview等)留出系统栏空间而不会被遮挡.
也可以在自定义主题中,增加windowTranslucentNavigation和windowTranslucentStatus属性。

Activity,Window和View之间的关系
        //了解 下面方法是获取 rootView
        Window win = activity.getWindow();
        ViewGroup decorViewGroup = (ViewGroup) win.getDecorView();

DecorView

SystemBarTint 项目

Apply background tinting to the Android system UI when using KitKat translucent modes.

//只对4.4做修改~修改代码:Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT){
}

设置4.4的Status bar颜色

第一步:定义主题Theme

values/
values-v19/


<style name="AppNavigationDrawerTheme" parent="@android:style/Theme.Holo.Light">
        <!--Translucent system bars-->
        <item name="android:windowTranslucentNavigation">false</item>
        <item name="android:windowTranslucentStatus">true</item>
    </style>

values-v21/


<style name="AppNavigationDrawerTheme" parent="@android:style/Theme.Holo.Light">
        <!--Translucent system bars-->
        <item name="android:windowTranslucentNavigation">true</item>
        <item name="android:windowTranslucentStatus">false</item>
    </style>

第二步:设置布局属性

//同样只对values-v19/设置

    android:fitsSystemWindows="@bool/fitsSystemWindows"
    android:clipToPadding="false"
第三步:设置颜色值

//同样只对Api19生效
//Activity

	@TargetApi(19)
	public static void setTranslucentStatus(Activity activity, boolean on) {
	if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
		SystemBarTintManager tintManager = new SystemBarTintManager(activity);
		tintManager.setStatusBarTintEnabled(true);
		tintManager.setNavigationBarTintEnabled(false);
		tintManager.setStatusBarTintColor(activity.getResources().getColor(R.color.colorPrimary));
	}
}

//ActionBarActivity

    @TargetApi(19)
    public void setTranslucentStatus(Activity activity, boolean on) {
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
        Window win = activity.getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);

        SystemBarTintManager tintManager = new SystemBarTintManager(activity);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setNavigationBarTintEnabled(true);
        tintManager.setStatusBarTintColor(activity.getResources().getColor(R.color.colorPrimary));
        tintManager.setNavigationBarTintColor(activity.getResources().getColor(R.color.colorPrimary));
        tintManager.setStatusBarTintResource(R.color.colorPrimary);
    }
}

搞定收工

GitHub地址:https://github.com/captain-miao/AndroidStartupDemo
apk下载地址:http://fir.im/yan

遇到的问题

Android 4.4 实现透明状态栏Translucent system bar(Status bar)》有1个想法

发表评论

电子邮件地址不会被公开。 必填项已用*标注