android在activity内禁止状态栏下拉

需求:要做一个全屏界面,同时在全屏状态下,禁止用户下拉状态栏。

代码如下:

    //禁止下拉
    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);
    }
    CustomViewGroup view;
    WindowManager manager;
    //允许下拉
    private void allowDropDown(){
        manager.removeView(view);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mymain);
        .....
        prohibitDropDown();
        .....
    }
        @Override
    protected void onDestroy() {
        super.onDestroy();
        .....
        allowDropDown();
        .....
    }

记得在界面关闭或最小化时调用allowDropDown()方法,否则在其他应用中也无法下拉状态栏了

来源:http://stackoverflow.com/questions/25284233/prevent-status-bar-for-appearing-android-modified

你可能感兴趣的:(小知识点)