给布局文件根控件设置padding即可,以下是Kotlin代码:
setContentView(R.layout.activity_home)
homeLayout!!.setPadding(0, BaseActivity.getStatusHeight(context), 0, BaseActivity.getBottomStatusHeight(context))
BaseActivity尚未转换为Kotlin:
/**
* 获得状态栏的高度
*/
public static int getStatusHeight(Context context) {
int statusHeight = -1;
try {
Class<?> clazz = Class.forName("com.android.internal.R$dimen");
Object object = clazz.newInstance();
int height = Integer.parseInt(clazz.getField("status_bar_height")
.get(object).toString());
statusHeight = context.getResources().getDimensionPixelSize(height);
} catch (Exception e) {
e.printStackTrace();
}
return statusHeight;
}
/**
* 获取 虚拟按键的高度
*/
public static int getBottomStatusHeight(Context context) {
int totalHeight = getDpi(context);
int contentHeight = getScreenHeight(context);
return totalHeight - contentHeight;
}