android 系统状态栏的隐藏和显示
系統狀態欄的隱藏和顯示。
昨天遇到這個問題,找了好久資料才解決,這里記錄一下。
網上流傳著很多種做法。比如:
1、在AndroidManifest.xml文件中修改theme為android:theme=”@android:style/Theme.NoTitleBar.Fullscreen”
2、在setContentView方法前執行如下代碼:
requestWindowFeature(Window.FEATURE_NO_TITLE)
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
3、通過View的setSystemUiVisibility方法
4、通過如下代碼實現狀態欄的隱藏和顯示:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) //隱藏狀態欄
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) //顯示狀態欄
方法1、2只能消除狀態欄,不能顯示狀態欄。
方法3,我采用過,調用setSystemUiVisibility方法,該方法傳入的參數可以為:
這里我需要傳入的是View.SYSTEM_UI_FLAG_FULLSCREEN,可是當我傳入該參數后,結果是:只是狀態欄消失了,但是位置還在。(測試手機:華為榮耀8 系統是基于Android 7.0的EMUI 5.0;三星galaxy s6 系統是Android 6.0)
最終,只有方法4可以正常使用。
下面記錄一下步驟:
1.顯示與隱藏狀態欄的代碼如下:
private void fullscreen(boolean enable) {
? ? ? ? if (enable) { //顯示狀態欄
? ? ? ? ? ? WindowManager.LayoutParams lp = getWindow().getAttributes();
? ? ? ? ? ? lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
? ? ? ? ? ? getWindow().setAttributes(lp);
? ? ? ? ? ? getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
? ? ? ? } else { //隱藏狀態欄
? ? ? ? ? ? WindowManager.LayoutParams lp = getWindow().getAttributes();
? ? ? ? ? ? lp.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
? ? ? ? ? ? getWindow().setAttributes(lp);
? ? ? ? ? ? getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
? ? ? ? }
? ? }
2.示與隱藏的效果如下:
步驟閱讀
3.橫屏與豎屏切換時處理狀態欄的顯示與隱藏,所以需要配置屏幕切換代碼。
AndroidManifest.xml中activity上配置
android:configChanges="orientation|keyboardHidden|screenSize"
4.activity中override方法onConfigurationChanged
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
? ?if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE ){//橫屏
? ?
? ?}else if( this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT ){//豎屏
? ?
? ?}
}
注意事項
- AndroidManifest.xml中activity配置android:configChanges="orientation|keyboardHidden|screenSize"
- override方法onConfigurationChanged
android 系統狀態欄的隱藏和顯示就講完了。
就這么簡單。
總結
以上是生活随笔為你收集整理的android 系统状态栏的隐藏和显示的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android Studio 解决The
- 下一篇: android Math的使用