【问题标题】:How to disable Android status bar interaction如何禁用Android状态栏交互
【发布时间】:2015-05-15 09:42:59
【问题描述】:

有没有办法在 android 上保留状态栏,同时禁用你可以用它做的所有交互,比如把它拉下来?

我想保留此栏提供的信息,但我不希望用户与之交互。

【问题讨论】:

  • 状态栏是指最上面的黑条,包含时间、电池等?
  • 是的,这个。这不是正确的名字吗?
  • 可能是这样,我只是想在输入内容之前先弄清楚。

标签: android android-statusbar


【解决方案1】:

这是我喜欢使用的方法。您可以从方法中解开它并将其放置在基本 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;
    }
}

还有!当您的活动结束时,您还必须删除此视图,因为我认为即使您终止应用程序,它也会继续阻塞屏幕。总是,总是 ALWAYS 调用这个 onPause 和 onDestroy。

    if (lockView!=null) {
        if (lockView.isShown()) {
            //unlock top
            manager.removeView(lockView);
        }
    }

【讨论】:

  • 我明白了。由于某些未知原因,我的观点已经使用 LAYOUT_IN_SCREEN,所以我只需要在屏幕顶部添加一条透明线。 50 的高度在所有设备上都相同吗?
  • 高度会随着设备分辨率的变化而变化,制造商也一样..所以所有设备都不一样
  • 有没有办法在运行时获取这个栏的大小?
  • 没关系,我在 stackoverflow 的其他地方找到了他的信息。
猜你喜欢
  • 2013-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多