遇到的問題: 雖然狀態欄隱藏了,可是原狀態欄位置上會被黑條佔領。
解決方案
- 作項目爲了達到全面屏顯示,設置了一個theme主題樣式
<style name="FullTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!--設置頂部狀態欄是否爲透明-->
<item name="android:windowTranslucentStatus">false</item>
<!--Android 5.x開始須要把顏色設置透明,不然頂部導航欄會呈現系統默認的淺灰色-->
<item name="android:statusBarColor">@android:color/transparent</item>
<!--設置頂部狀態欄和底部導航欄是否爲透明-->
<item name="android:windowTranslucentNavigation">true</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
複製代碼
<activity
android:theme="@style/FullTheme"
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
複製代碼
- MainActivity在super.onCreate(savedInstanceState); 調用hideStatusBar() hideBottomUIMenu()
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
hideStatusBar();
hideBottomUIMenu();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 隱藏狀態欄
*/
protected void hideStatusBar(){
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
getWindow().setAttributes(lp);
}
/**
* 隱藏虛擬按鍵,而且全屏
*/
protected void hideBottomUIMenu() {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) {
View v = this.getWindow().getDecorView();
v.setSystemUiVisibility(View.GONE);
} else if (Build.VERSION.SDK_INT >= 19) {
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY ;
decorView.setSystemUiVisibility(uiOptions);
}
}
}
複製代碼