Android 沉浸式状态栏

方法一:

在values-v19文件夹下声明AppTheme为透明状态栏,代码如下:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
     <!-- Customize your theme here. -->
     <item name="android:windowTranslucentStatus">true</item>
</style>

方法二:但是实际测试中发现在国产某些rom上,xml声明的会不起作用,在代码里直接声明更有效。在onCreate方法下声明。 

@Override
       protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState); 
            super.setContentView(R.layout.activity_base);
            // 经测试在代码里直接声明透明状态栏更有效
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
                localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
          }
     }

代码中声明有时会破坏界面的布局。

posted @ 2017-11-22 14:06  IT_lss  阅读(192)  评论(0编辑  收藏  举报