實現(xiàn)效果:

代碼實現(xiàn):
/**
* 是否將布局全屏顯示(布局填充到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) {
//根據(jù)資源ID獲取響應的尺寸值
? ? ? ? statusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
? ? }
return statusBarHeight;
}
調(diào)整頂部組件布局位置:
TextView tv? = ...;
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams)tv.getLayoutParams();
lp.topMargin = statusBarHeight;//設置view marginTop 的高度
toolbar.setLayoutParams(lp);