我需要知道动作栏的像素大小,以便应用正确的背景图像。


当前回答

@AZ13的回答很好,但根据Android设计指南,动作栏至少应该是48dp高。

其他回答

在新的v7支持库(21.0.0)中,R.dimen中的名称已更改为@dimen/ abc_action_bar_default_highight_material。

当从以前版本的支持库升级时,您应该使用该值作为动作栏的高度

@AZ13的回答很好,但根据Android设计指南,动作栏至少应该是48dp高。

我这样做是为了自己,这个帮手方法应该对其他人有用:

private static final int[] RES_IDS_ACTION_BAR_SIZE = {R.attr.actionBarSize};

/**
 * Calculates the Action Bar height in pixels.
 */
public static int calculateActionBarSize(Context context) {
    if (context == null) {
        return 0;
    }

    Resources.Theme curTheme = context.getTheme();
    if (curTheme == null) {
        return 0;
    }

    TypedArray att = curTheme.obtainStyledAttributes(RES_IDS_ACTION_BAR_SIZE);
    if (att == null) {
        return 0;
    }

    float size = att.getDimension(0, 0);
    att.recycle();
    return (int) size;
}

在我的Galaxy S4上有> 441dpi > 1080 x 1920 > 使用getResources()获取动作栏高度。我得到144像素。

使用公式px = dp x (dpi/160),我使用的是441dpi,而我的设备位于 在类别480dpi。这样就证实了这个结果。

如果你在使用ActionBarSherlock,你可以用

@dimen/abs__action_bar_default_height