您的位置:首页 > 移动开发 > Android开发

android activity 全屏非全屏切换

2016-06-15 14:39 681 查看
此文版权属于作者

所有,任何人、媒体或者网站转载、借用都必须征得作者本人同意!

android app 通过代码,在全屏和非全屏之间切换

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.view.WindowManager;

public class FullScreenOperator {
private static boolean sIsFullScreen = false;
private static int sOrientation = 0;
private static int sSystemUiVisibility = 0;
private static int sEnterCount = 0;

private static boolean isFullScreen(Activity activity) {
return WindowManager.LayoutParams.FLAG_FULLSCREEN
== (activity.getWindow().getAttributes().flags
& WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

public static void enter(Activity activity) {
++sEnterCount;

//Log.v(TAG, "enter " + sEnterCount + ", activity " + activity);

if (activity == null || sEnterCount != 1)
return;

sIsFullScreen = isFullScreen(activity);
sOrientation = activity.getRequestedOrientation();

if (Build.VERSION.SDK_INT >= 14) {
sSystemUiVisibility = activity.getWindow().getDecorView().getSystemUiVisibility();
}

if (!sIsFullScreen) {
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);

if (Build.VERSION.SDK_INT >= 16) {
// View.SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004
activity.getWindow().getDecorView().setSystemUiVisibility(0x00000004);
}
}

public static void exit(Activity activity) {
--sEnterCount;

//Log.v(TAG, "exit " + sEnterCount + ", activity " + activity);

if (activity == null || sEnterCount != 0)
return;

if (!sIsFullScreen)
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

activity.setRequestedOrientation(sOrientation);

if (Build.VERSION.SDK_INT >= 14) {
activity.getWindow().getDecorView().setSystemUiVisibility(sSystemUiVisibility);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 全屏切换