android全屏与非全屏的切换设置

静态方法

1. 代码方式

在Activity类OnCreate方法中设置,代码如下

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);
    }

requestWindowFeature与getWindow().setFlags必须放在setContentView方法之前

2. 文件配置方式

AndroidManifest.xml文件中,找到属性,设置如下:

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

动态方法

1. Window方式

    if (mIsFullScreen){//设置为非全屏
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().setAttributes(lp);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }else{//设置为全屏
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        getWindow().setAttributes(lp);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }

2. View方式

    if (mIsFullScreen){//设置为非全屏     
        getWindow().getDecorView().
        setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);                                 
    }else{//设置为全屏
        getWindow().getDecorView().
        setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);              
    }

参考资料

http://blog.csdn.net/michaelpp/article/details/730230
http://www.cnblogs.com/satng/p/3690854.html

你可能感兴趣的:(android之路)