想实现透明页面,包括状态栏和导航栏都透明,点击事件还可以透传。
设置页面透明,使用主题
注册activity设置主题
<style name="TranslucentNoActionBarTheme" parent="Theme.AppCompat.NoActionBar">
<item name="windowActionBar">false</item>
<item name="android:windowContentTransitions">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsTranslucent">true</item>
</style>
设置状态栏和导航栏透明
直接调用下面方法,传当前activity即可
public void setNavAndStatusBarTransparent(Activity activity) {
if (activity == null) {
return;
}
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = activity.getWindow();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (window != null) {
View decorView = window.getDecorView();
//两个 flag 要结合使用,表示让应用的主体内容占用系统状态栏的空间
int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
decorView.setSystemUiVisibility(option);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
//导航栏颜色也可以正常设置
window.setNavigationBarColor(Color.TRANSPARENT);
}
} else {
if (window != null) {
WindowManager.LayoutParams attributes = window.getAttributes();
if (attributes != null) {
int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
attributes.flags |= flagTranslucentStatus;
attributes.flags |= flagTranslucentNavigation;
window.setAttributes(attributes);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
设置activity点击事件透传
注意要在onCreate方法的setContenView前添加如下代码
//设置activity点击透传
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.flags = WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
完美实现。
|