作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 android 上有没有一种方法可以保留状态栏,同时禁用您可以使用它进行的所有交互,比如将其下拉?
我想保留这个栏提供的信息,但我不希望用户与之交互。
最佳答案
这是我喜欢使用的方法。您可以将其从方法中解包,并将其放在基础 Activity 中。 iirc,我也是从 StackOverflow 得到的,但我没有记下它,所以我不确定原始帖子在哪里。
它基本上所做的是在顶部栏上放置一个透明覆盖层,以拦截所有触摸事件。到目前为止,它对我来说效果很好,看看它对你的效果如何。
您可能需要将此行放入 AndroidManifest:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
我在我的项目中有它,但我不记得是因为这个还是其他原因。如果您收到权限错误,请将其添加。
WindowManager manager;
CustomViewGroup lockView;
public void lock(Activity activity) {
//lock top notification bar
manager = ((WindowManager) activity.getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE));
WindowManager.LayoutParams topBlockParams = new WindowManager.LayoutParams();
topBlockParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
topBlockParams.gravity = Gravity.TOP;
topBlockParams.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;
topBlockParams.width = WindowManager.LayoutParams.MATCH_PARENT;
topBlockParams.height = (int) (50 * activity.getResources()
.getDisplayMetrics().scaledDensity);
topBlockParams.format = PixelFormat.TRANSPARENT;
lockView = new CustomViewGroup(activity);
manager.addView(lockView, topBlockParams);
}
而 CustomViewGroup 是
private class CustomViewGroup extends ViewGroup {
Context context;
public CustomViewGroup(Context context) {
super(context);
this.context = context;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.i("StatusBarBlocker", "intercepted by "+ this.toString());
return true;
}
}
还有!您还必须在 Activity 结束时删除此 View ,因为我认为即使您终止了应用程序,它也会继续阻塞屏幕。总是,总是总是调用这个 onPause 和 onDestroy。
if (lockView!=null) {
if (lockView.isShown()) {
//unlock top
manager.removeView(lockView);
}
}
关于android - 如何禁用Android状态栏交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30256257/
我是一名优秀的程序员,十分优秀!