自定义 View 设置为全屏时的高度比 DisplayMetrics 获得的高度大

通过使用代码:

DisplayMetrics dm = getResources().getDisplayMetrics();
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;

Log.e(TAG, "W = " + dm.widthPixels + ", H = " + dm.heightPixels);

获得到屏幕的宽和高,在我测试的手机上,screenWidth = 1440screenHeight = 2400
屏幕宽高


然后,自定义 View,设置 layout_widthlayout_height 都是 match_parent

android:layout_width="match_parent"
android:layout_height="match_parent"

onDraw 方法里面,输出该 View 的 width 和 height:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    W = this.getWidth();
    H = this.getHeight();

    Log.e("RegionSelectorView", "W: " + W + ", H: " + H);
}

发现的输出都是:
onDraw 的 Log 输出

请问大家这是为什么,该如何解决这个不一致的问题?

阅读 2.9k
1 个回答

没记错的话,第一个方法不包含状态栏高度

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题