安卓禁止下拉菜单栏

1.首先创建一个类。

package com.Slq.VideoPlayer;

import android.content.Context;
import android.view.MotionEvent;
import android.view.ViewGroup;

/**
 * Created by 15265 on 2017/11/6.
 */

class CustomViewGroup extends ViewGroup {

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

    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {

    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return true;
    }
}


2.然后在需要禁止下拉菜单栏的窗口类中或者单独一个类中编写禁止和允许下拉菜单栏的方法。

需要声明的属性

CustomViewGroup view;
WindowManager manager;


禁止下拉的方法

private void prohibitDropDown() {
    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;
    view = new CustomViewGroup(this);
    manager.addView(view, localLayoutParams);
}

允许下拉的方法

private void allowDropDown(){
    manager.removeView(view);
}


3.然后需要在开始方法(onCreate)中写上禁止下拉的方法。

if (! Settings.canDrawOverlays(UnityPlayerActivity.this)) {
    Intent intent = new Intent(Settings.
            ACTION_MANAGE_OVERLAY_PERMISSION,
            Uri.parse("package:" + getPackageName()));
    startActivity(intent); }

prohibitDropDown();


4.在结束方法(onDestroy)中写上允许下拉的方法,否则的话在其他程序和界面的时候也不能下拉菜单栏。


5.最后需要在配置文件(AndroidMainFest)中申请权限。

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值