Toolbar相关
设置状态栏透明,并保持住 toolbar 在status之下(支持 API 19 以上):
- 添加到 theme:
<item name="android:windowTranslucentStatus">true</item>
- 使 toolbar 的 marginTop 正好等于 statusHeight:
1
2
3
4
5
6
7
8
9
10public static void setToolbarMarginTop(Activity activity, Toolbar toolbar){
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams();
params.topMargin = getStatusBarHeight(activity);
}
public static int getStatusBarHeight(Activity activity){
int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
int statusBarHeight = activity.getResources().getDimensionPixelSize(resourceId);
return statusBarHeight;
}