android 沉浸式状态栏
2015-07-19 19:32
519 查看
针对5.0(api21)
首先定义主题
在values-v21的styles.xml中定义
然后在根布局文件中定义
一定要使用DrawerLayout,以及使用属性android:fitsSystemWindows="true"
以上即可在5.0机器上实现沉浸式状态栏
使用DrawerLayout的原因分析
在DrawerLayout的构造函数中,有此判断获取状态栏的背景
和前面style中定义的主题对应,然后再onDraw方法中画出来
以上实现,局限性非常大,以后研究使用普通的View来实现,以及在4.4中的实现。
首先定义主题
<resources> <!-- Base application theme. --> <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- tool_bar(actionbar)颜色 --> <item name="colorPrimary">#4876FF</item> <!-- 状态栏颜色 --> <item name="colorPrimaryDark">#4876FF</item> <!-- 窗口的背景颜色 --> <item name="android:windowBackground">@android:color/white</item> <item name="colorAccent">#ffff5722</item> </style> <style name="AppTheme" parent="AppBaseTheme"></style> </resources>
在values-v21的styles.xml中定义
<resources> <style name="AppTheme" parent="AppBaseTheme"> <!-- 底部导航栏颜色 --> <item name="android:navigationBarColor">#4876FF</item> <item name="android:windowTranslucentNavigation">true</item> <!-- 沉浸式的statubar,先将statusBar设置为透明,然后Activity的根布局为DrawerLayout,设置布局DrawerLayout的属性android:fitsSystemWindows="true" 将colorPrimary和colorPrimaryDark设置成一样,否则分割线很明显--> <item name="android:statusBarColor">@android:color/transparent</item> <item name="android:windowDrawsSystemBarBackgrounds">true</item> <item name="android:windowTranslucentStatus">true</item> <item name="android:windowContentTransitions">true</item> </style> </resources>
然后在根布局文件中定义
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/abs_screen_drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <LinearLayout android:id="@+id/screen_content_parent" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include android:id="@+id/toolbar" layout="@layout/tool_bar" /> </LinearLayout> </android.support.v4.widget.DrawerLayout>
一定要使用DrawerLayout,以及使用属性android:fitsSystemWindows="true"
以上即可在5.0机器上实现沉浸式状态栏
使用DrawerLayout的原因分析
在DrawerLayout的构造函数中,有此判断获取状态栏的背景
if (ViewCompat.getFitsSystemWindows(this)) { IMPL.configureApplyInsets(this); mStatusBarBackground = IMPL.getDefaultStatusBarBackground(context); }
public static Drawable getDefaultStatusBarBackground(Context context) { final TypedArray a = context.obtainStyledAttributes(THEME_ATTRS); try { return a.getDrawable(0); } finally { a.recycle(); } }
private static final int[] THEME_ATTRS = { android.R.attr.colorPrimaryDark };
和前面style中定义的主题对应,然后再onDraw方法中画出来
@Override public void onDraw(Canvas c) { super.onDraw(c); if (mDrawStatusBarBackground && mStatusBarBackground != null) { final int inset = IMPL.getTopInset(mLastInsets); if (inset > 0) { mStatusBarBackground.setBounds(0, 0, getWidth(), inset); mStatusBarBackground.draw(c); } } }
以上实现,局限性非常大,以后研究使用普通的View来实现,以及在4.4中的实现。
相关文章推荐
- android发展历史简介
- Android应用加解密--AndroidManifest文件加解密
- android RelativeLayout 内容居中解决办法:
- 深入Android【八】 —— Activity间数据传输
- Android序列化
- 50一个Android开发技巧(01 利用好layout_weight属性)
- Android 四大组件学习之Activity五
- 深入Android【七】 —— 资源文件
- 深入Android 【六】 —— 界面构造
- Android开源项目:贴纸标签相机,快给你的图片贴上贴纸打上标签吧!~
- android截图
- android自定义组件流程
- Android开发那些事
- 深入Android 【一】 —— 序及开篇
- Android中dip、dp、sp、pt和px的区别
- Android 中使用Lambda表达式
- Android中打开raw目录下的数据库文件
- 【Android】资源系列(二) -- 文件原样保留的资源assets和res/raw目录
- 我的Android进阶之旅------>Android字符串资源中的单引号问题error: Apostrophe not preceded by 的解决办法
- android介绍