全屏显示(隐藏状态栏和导航栏)
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(lp);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
/**
* 图片详情页显示全屏
*/
public void enterFullScreen(View view) {
//addContentView将view添加的activity
addContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
//状态栏还会显示,需要隐藏状态栏
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(lp);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
退出全屏(还原状态栏和导航栏)
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setAttributes(lp);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
/**
* 退出全屏
* @param view
*/
public void exitFullScreen(View view) {
//activity没有removeContentView,需要通过view的parent来移除
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null) {
parent.removeView(view);
}
//退出还原状态栏
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setAttributes(lp);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
切换全屏
/**
* 切换全屏
*/
private void toggleFullScreen() {
//fragment显示在哪个activity,获取到的就是那个activity
MainActivity activity = (MainActivity) getActivity();
if (alreadyShowFullScreen) {
activity.exitFullScreen(mFullScreenViewPager);
} else {
activity.enterFullScreen(mFullScreenViewPager);
}
alreadyShowFullScreen = !alreadyShowFullScreen;
}