我想在工具栏下方显示一个ui,但问题是我的工具栏大小是50dp,如果我说我的布局边距从前50名然后对于某些Android版本它的工作。我认为在api 22之上我们还需要status_bar_height,其中包括工具栏高度。 获取工具栏高度的最佳方法是什么,以便我可以在工具栏上方显示视图。我正在显示一个透明的覆盖屏幕
int statusBar = getStatusBarHeight();
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(0, (statusBar + 50), 0, 0);
MainRel.setLayoutParams(layoutParams);
MainRel.setVisibility(View.VISIBLE);
}
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
答案 0 :(得分:0)
使用此通用代码。覆盖OnWindowFocusChanged
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
int toolbarHeight = toolbar.getHeight();
int statusBar = getStatusBarHeight();
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(0, (statusBar + toolbarHeight), 0, 0);//you can use the toolbar height you got here.
MainRel.setLayoutParams(layoutParams);
MainRel.setVisibility(View.VISIBLE);
}