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" />