2015-10-07 26 views
2

我使用隱藏狀態欄全屏目的下面的代碼:隱藏狀態欄或奇巧與全屏

void HideEverything(){ 
     if (Build.VERSION.SDK_INT < 16) { 
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     } else { 
      View decorView = getWindow().getDecorView(); 
      // Hide the status bar. 
      int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN; 
      decorView.setSystemUiVisibility(uiOptions); 
      // Remember that you should never show the action bar if the 
      // status bar is hidden, so hide that too if necessary. 
      ActionBar actionBar = getActionBar(); 
      actionBar.hide(); 
     } 
    } 

它顯示了在全屏模式下的屏幕,但是當我接觸的狀態欄它顯示狀態欄。 我嘗試了其他選項來添加全屏幕和應用程序和活動級別清單中的標題欄主題,但仍然結果是一樣的...... :(

我不想在任何步驟顯示狀態欄。 如何在Android中實現這一

編輯:

Android操作系統版本4.4.2是 (上面的代碼工作在4.3,但不是4.4.2)

更新:

問題已經解決... 答案分別寫在下面......

回答

6

沒有解決辦法的問題作爲工作的Android版本是4.4.2。

作爲評論由程序員的@Academy;此解決方案無法在Android 6及更高版本上運行。

但按照Sunny的建議,問題已經通過阻止狀態欄的擴展來解決。

解決辦法是:

寫一個內嵌customViewGroup類的主要活動。

public class customViewGroup extends ViewGroup { 

     public customViewGroup(Context context) { 
      super(context); 
     } 

     @Override 
     protected void onLayout(boolean changed, int l, int t, int r, int b) { 
     } 

     @Override 
     public boolean onInterceptTouchEvent(MotionEvent ev) { 
      Log.v("customViewGroup", "**********Intercepted"); 
      return true; 
     } 
    } 

現在,在您的mainActivity添加以下代碼onCreate方法之前setContentView()

WindowManager manager = ((WindowManager) getApplicationContext() 
       .getSystemService(Context.WINDOW_SERVICE)); 

     WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams(); 
     localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR; 
     localLayoutParams.gravity = Gravity.TOP; 
     localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE| 

       // this is to enable the notification to receive touch events 
       WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | 

       // Draws over status bar 
       WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN; 

     localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT; 
     localLayoutParams.height = (int) (50 * getResources() 
       .getDisplayMetrics().scaledDensity); 
     localLayoutParams.format = PixelFormat.TRANSPARENT; 

     customViewGroup view = new customViewGroup(this); 

     manager.addView(view, localLayoutParams); 

上面的代碼將禁用狀態欄的擴張。 現在充分利用屏幕下面的代碼,添加它僅低於上面的代碼和setContentView前:

//fullscreen 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); 

現在清單中添加的權限:

<!-- prevent expanding status bar --> 
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> 

和你做.... :)

缺點:此代碼disables expansion of status bar for the device不只是一個活動或一個應用程序,所以使用它,如果你真的需要它。

感謝您所有的解決方案和建議... :)

+1

這是非常好的解決方案,特別是當你需要在全屏顯示一些動畫,並且完成後,刪除已創建的視圖。感謝您提供此代碼。 – DoubleK

+0

@Jyo the Whiff這太好了。你知道我如何禁用軟鍵(Back/Home?Recent App)嗎? – Dave

+0

當WindowManager.LayoutParams.TYPE_SYSTEM_ERROR的功能消失時,此解決方案在即將到來的Android 8版本中將不再適用。請參閱https://www.bleepingcomputer.com/news/security/android-o-will-contain-special-feature-to-fight-off-ransomware/ – Redwolf

3

我們不能阻止出現在奇巧或以上設備的全屏模式下的狀態,所以在嘗試了黑客阻擋在狀態欄從擴大。

對於一個想法,覆蓋狀態欄並消耗所有輸入事件。它會阻止狀態欄擴展。

+1

是啊,這對do..And也指出@Sunny – DJphy

+1

謝謝你的建議的方式......我已經解決了這個問題,添加了答案... :) –

0

使用下面的代碼的onCreate

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
requestWindowFeature(Window.FEATURE_NO_TITLE); 
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); 
} 
2

使用下面的代碼,你可以使用全屏幕;

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 

在setcontentview之上寫下面的代碼。

注意:不要給任何主題,本次活動。

希望這會有所幫助。