這是一個非常古老的問題,但經過這麼多的努力,我仍然無法弄清楚一個可能的解決方案。Android 5.0,防止狀態欄擴展
目的
其實很簡單。我想開發一些全屏應用程序(例如遊戲),其中狀態欄不展開,但用戶觸摸屏幕。
該應用應該能夠在Android 5.0,sdk 21(Lollipop)上工作。
我做了什麼至今
我發現this post,其中介紹瞭如何蓋上CustomView
吸收觸摸事件的狀態欄,但它不爲我工作...
我甚至改變了CustomView
的大小,使它覆蓋整個屏幕,但它仍然無法工作,它甚至不能覆蓋我的按鈕。
我使用的代碼附在下面。在設備上運行時,按鈕和狀態欄都可以正常工作 - 沒有任何內容被阻止。
我錯過了什麼嗎?
代碼
在AndroidManifest.xml:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
activity_main.xml中:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/text_show" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
android:id="@+id/button_click"
android:onClick="changeText"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
在MainActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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 recieve 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(R.layout.activity_main);
}
boolean key = false;
public void changeText(View view) {
TextView tv = (TextView) findViewById(R.id.text_show);
if (key) {
tv.setText("hahaha");
} else {
tv.setText("eroero");
}
key = ! key;
}
在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;
}
}
我想你需要從'onInterceptTouchEvent'方法返回** false **。 –
@SweetWisherツ從這裏:https://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent(android.view.MotionEvent)它說:「返回真正的從孩子偷動作事件,並讓他們通過onTouchEvent()分派給這個ViewGroup,當前目標會收到一個ACTION_CANCEL事件,這裏不會傳遞進一步的消息。「 – WhatsUp
你試過了嗎? –