1.
Demo:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
getWindow().getDecorView().setSystemUiVisibility(View.INVISIBLE);
}
else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// this.requestWindowFeature(Window.f);// 去掉標題欄
// this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
// WindowManager.LayoutParams.FLAG_FULLSCREEN);// 去掉信息欄
Log.i("info", "portrait"); // 豎屏
}
Need:
<uses-sdk android:minSdkVersion="11" />
TD:
View類提供了setSystemUiVisibility和getSystemUiVisibility方法,這兩個方法實現對狀態欄的動態顯示或隱藏的操作,以及獲取狀態欄當前可見性。
setSystemUiVisibility(int visibility)方法可傳入的實參為:
1. View.SYSTEM_UI_FLAG_VISIBLE:顯示狀態欄,Activity不全屏顯示(恢復到有狀態的正常情況)。
2. View.INVISIBLE:隱藏狀態欄,同時Activity會伸展全屏顯示。
3. View.SYSTEM_UI_FLAG_FULLSCREEN:Activity全屏顯示,且狀態欄被隱藏覆蓋掉。
4. View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN:Activity全屏顯示,但狀態欄不會被隱藏覆蓋,狀態欄依然可見,Activity頂端布局部分會被狀態遮住。
5. View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION:效果同View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
6. View.SYSTEM_UI_LAYOUT_FLAGS:效果同View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
7. View.SYSTEM_UI_FLAG_HIDE_NAVIGATION:隱藏虛擬按鍵(導航欄)。有些手機會用虛擬按鍵來代替物理按鍵。
8. View.SYSTEM_UI_FLAG_LOW_PROFILE:狀態欄顯示處於低能顯示狀態(low profile模式),狀態欄上一些圖標顯示會被隱藏。
2.
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(attrs);
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setAttributes(attrs);
getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}