若干方法划分,百分比或重量的屏幕高度的编程改变AppBarLayout高度:
private AppBarLayout appbar;
/**
* @return AppBarLayout
*/
@Nullable
protected AppBarLayout getAppBar() {
if (appbar == null) appbar = (AppBarLayout) findViewById(R.id.appbar);
return appbar;
}
/**
* @param divide Set AppBar height to screen height divided by 2->5
*/
protected void setAppBarLayoutHeightOfScreenDivide(@IntRange(from = 2, to = 5) int divide) {
setAppBarLayoutHeightOfScreenPercent(100/divide);
}
/**
* @param percent Set AppBar height to 20->50% of screen height
*/
protected void setAppBarLayoutHeightOfScreenPercent(@IntRange(from = 20, to = 50) int percent) {
setAppBarLayoutHeightOfScreenWeight(percent/100F);
}
/**
* @param weight Set AppBar height to 0.2->0.5 weight of screen height
*/
protected void setAppBarLayoutHeightOfScreenWeight(@FloatRange(from = 0.2F, to = 0.5F) float weight) {
if (getAppBar() != null) {
ViewGroup.LayoutParams params = getAppBar().getLayoutParams();
params.height = Math.round(getResources().getDisplayMetrics().heightPixels * weight);
getAppBar().setLayoutParams(params);
}
}