公司要求状态栏设置图片填充。在网上查找资料,实现以后发现华为手机底部虚拟键为灰色。于是又开始在网上查找资料,找了好多方法都不行,或者实现很麻烦,最后试出了一种简单的方法。
先在styles.xml文件中设置状态栏图片填充
<!--设置状态栏图片填充-->
<style name="ImageTranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowTranslucentStatus" tools:ignore="NewApi">false</item>
<item name="android:windowTranslucentNavigation" tools:ignore="NewApi">true</item>
<!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->
<item name="android:statusBarColor" tools:ignore="NewApi">@android:color/transparent</item>
</style>
然后再AndroidManifest.xml中的activity设置
android:theme="@style/ImageTranslucentTheme"
<activity
android:name=".view.ui.activity.LoginActivity"
android:launchMode="singleTask"
android:theme="@style/ImageTranslucentTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
最后在需要设置的Activity中设置
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
window.setNavigationBarColor(Color.TRANSPARENT);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
}
之前的效果
之后的效果