Android沉浸式状态栏的实现
2020-05-02 本文已影响0人
资本家大恶人
在MainActivity中的onCreate方法
requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = getWindow();
window.getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.parseColor("#00000000"));//根据顶部控件该颜色
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
ViewGroup contentFrameLayout = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);
View parentView = contentFrameLayout.getChildAt(0);
if (parentView != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
parentView.setFitsSystemWindows(true);
}
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorWrite</item>
<item name="colorPrimaryDark">@color/colorWrite</item>
<item name="colorAccent">@color/colorWrite</item>
<item name="android:windowIsTranslucent">true</item>
</style>
透明状态栏使用方法
1.在res文件夹下创建style-V21文件
2.复制style文件
<item name="android:statusBarColor">#00ffffff</item>