android-沉浸状态栏以及状态栏背景色如何设置( 二 )


在该的布局文件中根元素下设置:
:=”true”
:=”true”
就可以了 。
效果图:

android-沉浸状态栏以及状态栏背景色如何设置

文章插图

android-沉浸状态栏以及状态栏背景色如何设置

文章插图
设置之后,间距是不是达到了我们想要的距离了,是正常的距离 。
但是新问题出现了,状态栏颜色成了系统默认的颜色背景了 。如何设置呢?简单!!!只要把上面的
:=”true”
:=”true”
这两句代码放在布局文件的根元素的第一个元素下面就可以了 。例如我的上面的界面的根元素是,他的第一个元素是标题栏,一个返回按钮,一个图片按钮,可以把上面的两句代码放在标题栏的布局中,例如:
........................
这样设置之后,效果图:
正常了些 。整个背景都是想要的了 。
至于如何设置状态栏中的字体颜色,从开源中国论坛中看到了一个牛人的代码,现在粘贴在此,我还没看明白,目测是从源码的角度来写的,废话不多说,代码如下;
public class SystemBarTintManager {/*** The default system bar tint color value.*/public static final int DEFAULT_TINT_COLOR = 0x99000000;private final SystemBarConfig mConfig;private boolean mStatusBarAvailable;private boolean mNavBarAvailable;private boolean mStatusBarTintEnabled;private boolean mNavBarTintEnabled;private View mStatusBarTintView;private View mNavBarTintView;private static boolean sIsMiuiV6;private static String sNavBarOverride = null;static {Method methodGetter = null;try {Class sysClass = Class.forName("android.os.SystemProperties");methodGetter = sysClass.getDeclaredMethod("get", String.class);sIsMiuiV6 = "V6".equals((String) methodGetter.invoke(sysClass, "ro.miui.ui.version.name"));} catch (Exception e) {e.printStackTrace();}finally{if(methodGetter!=null){try {sNavBarOverride = (String) methodGetter.invoke(null, "qemu.hw.mainkeys");} catch (Exception e) {e.printStackTrace();sNavBarOverride = null;}}}}/*** @param activity The host activity.*/@TargetApi(19)public SystemBarTintManager(Activity activity) {Window win = activity.getWindow();ViewGroup decorViewGroup = (ViewGroup) win.getDecorView();if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {// check theme attrsint[] attrs = {android.R.attr.windowTranslucentStatus,android.R.attr.windowTranslucentNavigation};TypedArray a = activity.obtainStyledAttributes(attrs);try {mStatusBarAvailable = a.getBoolean(0, false);mNavBarAvailable = a.getBoolean(1, false);} finally {a.recycle();}// check window flagsWindowManager.LayoutParams winParams = win.getAttributes();int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;if ((winParams.flags & bits) != 0) {mStatusBarAvailable = true;}bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;if ((winParams.flags & bits) != 0) {mNavBarAvailable = true;}}mConfig = new SystemBarConfig(activity, mStatusBarAvailable, mNavBarAvailable);// device might not have virtual navigation keysif (!mConfig.hasNavigtionBar()) {mNavBarAvailable = false;}if (mStatusBarAvailable) {setupStatusBarView(activity, decorViewGroup);}if (mNavBarAvailable) {setupNavBarView(activity, decorViewGroup);}}/*** Enable tinting of the system status bar.** @param enabled True to enable tinting, false to disable it (default).*/public void setStatusBarTintEnabled(boolean enabled) {mStatusBarTintEnabled = enabled;if (mStatusBarAvailable) {mStatusBarTintView.setVisibility(enabled ? View.VISIBLE : View.GONE);}}/*** set status bar darkmode* @param darkmode* @param activity*/public void setStatusBarDarkMode(boolean darkmode, Activity activity) {if (sIsMiuiV6) {Class