1. 程式人生 > 其它 >直播app原始碼,狀態列和導航欄設定成透明狀態

直播app原始碼,狀態列和導航欄設定成透明狀態

直播app原始碼,狀態列和導航欄設定成透明狀態實現的相關程式碼

設定頁面透明,使用主題

註冊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;

以上就是 直播app原始碼,狀態列和導航欄設定成透明狀態實現的相關程式碼,更多內容歡迎關注之後的文章