1、demo效果图
在android 4.4上 有一个新的特性可以设置手机状态栏的背景,让手机整个界面的风格保持一致,在今年的google i/o上的android l默认就是这种风格。来先看一下demo效果图:
图一:透明状态栏 图2:蓝色背景状态栏
2、整个DEMO的工程结构图如下所示:
3、主要的实现代码如下所示:
首先,要记得导入systembartint-1.0.4.jar包
3.1 MainActivity.java中的代码如下所示:
package com.example.testdemo; import android.annotation.TargetApi; import android.app.Activity; import android.os.Build; import android.os.Bundle; import android.view.Window; import android.view.WindowManager; import com.readystatesoftware.systembartint.SystemBarTintManager; public class MainActivity extends Activity { private boolean isBarTint = true; private boolean isTranslucentBarTint = false; public void setBarTint(boolean isBarTint) { this.isBarTint = isBarTint; } public void setTranslucentBarTint(boolean isTranslucentBarTint) { this.isTranslucentBarTint = isTranslucentBarTint; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (isBarTint) { initSystemBar(); } else if (isTranslucentBarTint) { initTranslucentSystemBar(); } } @TargetApi(Build.VERSION_CODES.KITKAT) private void initSystemBar() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { setTranslucentStatus(true); SystemBarTintManager tintManager = new SystemBarTintManager(this); // 激活状态栏设置 tintManager.setStatusBarTintEnabled(true); // 激活导航栏设置 tintManager.setNavigationBarTintEnabled(false); int sbg = getResources().getColor(R.color.titlebar_rl_background); tintManager.setTintColor(sbg); tintManager.setStatusBarTintColor(sbg); } } @TargetApi(Build.VERSION_CODES.KITKAT) private void initTranslucentSystemBar() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { setTranslucentStatus(true); SystemBarTintManager tintManager = new SystemBarTintManager(this); // 激活状态栏设置 tintManager.setStatusBarTintEnabled(true); // 激活导航栏设置 tintManager.setNavigationBarTintEnabled(false); int sbg = getResources().getColor(R.color.viewfinder_title); tintManager.setTintColor(sbg); tintManager.setStatusBarTintColor(sbg); } } @TargetApi(Build.VERSION_CODES.KITKAT) private void setTranslucentStatus(boolean isOpen) { Window win = getWindow(); WindowManager.LayoutParams winParams = win.getAttributes(); final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; if (isOpen) { winParams.flags |= bits; } else { winParams.flags &= ~bits; } win.setAttributes(winParams); } @TargetApi(Build.VERSION_CODES.KITKAT) private void setTranslucentNavigation(boolean isOpen) { Window win = getWindow(); WindowManager.LayoutParams winParams = win.getAttributes(); final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION; if (isOpen) { winParams.flags |= bits; } else { winParams.flags &= ~bits; } win.setAttributes(winParams); } }
3.2 colors.xml的颜色值如下所示:
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="titlebar_rl_background">#33b5e6</color> <color name="viewfinder_title">#80000000</color> </resources>
3.3 styles.xml中的代码如下所示:
<resources> <style name="AppTheme" parent="AppBaseTheme"> <item name="android:windowNoTitle">true</item> <item name="android:fitsSystemWindows">true</item> </style> </resources>
3.4 AndroidManifest.xml 中application或对应的activity设置主题 android:theme="@style/AppTheme"
时间: 2024-10-07 01:23:31