高度值:DecorView、ContentView、ActionBar、StatusBar、Screen、NavigationBar

//测试代码
@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    Log.d(TAG, "zwm, DecorView height: " + getWindow().getDecorView().getHeight());
    Log.d(TAG, "zwm, ContentView height: " + (getWindow().getDecorView().findViewById(android.R.id.content)).getHeight());
    Log.d(TAG, "zwm, ActionBar height: " + getActionBar().getHeight());
    Log.d(TAG, "zwm, StatusBar height: " + getStatusBarHeight());
    Log.d(TAG, "zwm, Screen height: " + dm.heightPixels);
    Log.d(TAG, "zwm, NavigationBar height: " + getNavigationBarHeight());
}

private int getStatusBarHeight(){
    int height = 0;
    int resourceId = this.getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        height = this.getResources().getDimensionPixelSize(resourceId);
    }
    return height;
}

private int getNavigationBarHeight() {
    int height = 0;
    int resourceId;
    int config = this.getResources().getIdentifier("config_showNavigationBar", "bool", "android");
    if (config != 0) {
        resourceId = this.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
        if(resourceId > 0) {
            height = this.getResources().getDimensionPixelSize(resourceId);
        }
    }
    return height;
}

//输出log
zwm, DecorView height: 2076
zwm, ContentView height: 1860
zwm, ActionBar height: 144
zwm, StatusBar height: 72
zwm, Screen height: 2076
zwm, NavigationBar height: 144

//结论
DecorView = ContentView + ActionBar + StatusBar
屏幕高度 = Screen + NavigationBar

你可能感兴趣的:(高度值:DecorView、ContentView、ActionBar、StatusBar、Screen、NavigationBar)