Android 状态栏沉浸式

来源:互联网 发布:淘宝不能下载 编辑:程序博客网 时间:2024/06/05 01:16

状态栏的设置需要区分Android版本,在Android 4.4以下 和Android 5.0以上设置的方式不一样

Android4.4以下版本:

方式一: 在xml中设置
res/values-v19

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">    <item name="android:windowTranslucentStatus">true</item>    <item name="android:windowTranslucentNavigation">true</item></style>

方式二: 代码设置

 //沉浸式 低版本        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {            Window window = getWindow();                    window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);        }

Android版本5.0以上

方式一: xml中设置
res/vaules-v21

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">    <item name="android:windowTranslucentStatus">false</item>    <item name="android:windowTranslucentNavigation">true</item>    <!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->    <item name="android:statusBarColor">@android:color/transparent</item></style>

方式二: 代码设置

        if (Build.VERSION.SDK_INT >= 21) {            View decorView = getWindow().getDecorView();            int option = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;            decorView.setSystemUiVisibility(option);            getWindow().setNavigationBarColor(Color.TRANSPARENT);            getWindow().setStatusBarColor(Color.TRANSPARENT);        }
原创粉丝点击