Androidで、v7.Toolbarに高さ指定するときについて

ActionBarをMaterial対応させると、Heightは
56dp (デフォルト)
48dp (横向き)
64dp (タブレット、sw600dp)
になると思うが、実際に直指定するのはださい。 ICSからNougatまでに対応し、なおかつ直指定しない方法として、

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/original" />

とすれば良さそう。

?android:attr/actionBarSize

ではなく

?attr/actionBarSize

と指定する。つまり、Compatの値を呼ぶようにする。

コードによる、actionBarSize の確認方法

// これは56dp
final TypedArray styledAttributes = getContext().getTheme().obtainStyledAttributes(
      new int[]{R.attr.actionBarSize});
final int px = (int) styledAttributes.getDimension(0, 0);
styledAttributes.recycle();
final int dp = px / (int) getResources().getDisplayMetrics().density;

Log.d("size", String.valueOf(dp));
// これだと48dpになる
final TypedArray styledAttributes = getContext().getTheme().obtainStyledAttributes(
      new int[]{android.R.attr.actionBarSize});
final int px = (int) styledAttributes.getDimension(0, 0);
styledAttributes.recycle();
final int dp = px / (int) getResources().getDisplayMetrics().density;

Log.d("size", String.valueOf(dp));