Android獲取狀態列高度,動態設定控制元件高度
阿新 • • 發佈:2019-02-16
獲取狀態列高度
//獲取status_bar_height資源的ID int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { //根據資源ID獲取響應的尺寸值 int statusBarHeight = getResources().getDimensionPixelSize(resourceId); SPUtils.putInt(Constant.STATUSBAR_HEIGHT,statusBarHeight); }
動態設定控制元件高度
int statusBarHeight = SPUtils.getInt(Constant.STATUSBAR_HEIGHT, 0);
if (statusBarHeight > 0) {
LinearLayout.LayoutParams sp_params = new LinearLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
sp_params.height = statusBarHeight ;
mvView.setLayoutParams(sp_params);
}
務必保持一致 ( LinearLayout. LinearLayout.LayoutParams 與需要設定的控制元件父控制元件)
LinearLayout.LayoutParams
<LinearLayoutandroid:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <View android:id="@+id/mv_view" android:layout_width="match_parent" android:layout_height="@dimen/statusbar_height" android:background="@color/colorTheme" android:visibility="visible" /> </LinearLayout>