沒有解決辦法的問題作爲工作的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
不只是一個活動或一個應用程序,所以使用它,如果你真的需要它。
感謝您所有的解決方案和建議... :)
這是非常好的解決方案,特別是當你需要在全屏顯示一些動畫,並且完成後,刪除已創建的視圖。感謝您提供此代碼。 – DoubleK
@Jyo the Whiff這太好了。你知道我如何禁用軟鍵(Back/Home?Recent App)嗎? – Dave
當WindowManager.LayoutParams.TYPE_SYSTEM_ERROR的功能消失時,此解決方案在即將到來的Android 8版本中將不再適用。請參閱https://www.bleepingcomputer.com/news/security/android-o-will-contain-special-feature-to-fight-off-ransomware/ – Redwolf