本文主要是介绍Android开发之沉浸式状态栏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
现在Android开发 很多软件的通知栏和程序的标题栏融为一体。其实我们平常说的沉浸式状态栏并不是真正的沉浸式,而是透明栏(Translucent Bars)。有兴趣的话可以看看这篇文章http://www.open-open.com/lib/view/open1472112617427.html。这里就不在细说了,只说一下怎么实现。
其实这种实现起来也很简单。这里是和toolbar结合使用
1、设置设置toolbar的样式
<style name="AppTheme_toolbar" parent="Theme.AppCompat.Light.NoActionBar"><!-- toolbar(actionbar)颜色 --><item name="colorPrimary">#4876FF</item><!-- 状态栏颜色 --><item name="colorPrimaryDark">#3A5FCD</item><!-- 窗口的背景颜色 --><item name="android:windowBackground">@android:color/white</item>
</style>
2、在Activity设置状态栏透明
//判断版本是否大于19,大于19才设置状态栏透明
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
这个方法只适用于系统版本19以上。并且设置在setContentView之前
3、修改布局文件中的根布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.singlethreaddownload.MainActivity"//添加下面两行代码android:fitsSystemWindows="true"android:clipToPadding="true">
如果不添加就会让toolbar与状态栏重合。如果想让状态栏的颜色随着toolbar的颜色变化,侧需要在toolbar中添加这两行代码
<android.support.v7.widget.Toolbarandroid:id="@+id/activity_main_toolbar"android:layout_width="match_parent"android:fitsSystemWindows="true"android:clipToPadding="true"android:layout_height="wrap_content"app:theme="@style/ToolbarTheme"android:minHeight="?attr/actionBarSize"android:background="?attr/colorPrimary">
这篇关于Android开发之沉浸式状态栏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!