实现效果:
代码实现:
/**
* 是否将布局全屏显示(布局填充到statusbar上面)
* @param window
* @param isFullScreen
*/
public static void setFullScreenToStatusBar(Window window,boolean isFullScreen) {
if (!isFullScreen) {
window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//全屏显示
window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);//强制
}else {
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
}
}
注意:使用了全屏显示以后布局会与覆盖到通知栏,可使用以下代码适应通知栏
获取通知栏高度
public static int getStatusBarHeight(Context context) {
if (statusBarHeight != -1) {
return statusBarHeight;
}
//获取status_bar_height资源的ID
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId >0) {
//根据资源ID获取响应的尺寸值
statusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
}
return statusBarHeight;
}
调整顶部组件布局位置:
TextView tv = ...;
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams)tv.getLayoutParams();
lp.topMargin = statusBarHeight;//设置view marginTop 的高度
toolbar.setLayoutParams(lp);