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


当前回答

为了获得动作栏的实际高度,你必须在运行时解析actionBarSize属性。

TypedValue tv = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
int actionBarHeight = getResources().getDimensionPixelSize(tv.resourceId);

其他回答

为了获得动作栏的实际高度,你必须在运行时解析actionBarSize属性。

TypedValue tv = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
int actionBarHeight = getResources().getDimensionPixelSize(tv.resourceId);

我需要在一个pre-ICS兼容应用程序中正确复制这些高度,并深入研究框架核心源代码。以上两个答案都是正确的。

它基本上可以归结为使用限定词。高度由维度"action_bar_default_height"定义

默认定义为48dip。但是对于-land是40dip,对于sw600dp是56dip。

如果你正在使用最近的v7 appcompat支持包中的兼容ActionBar,你可以使用

@dimen/abc_action_bar_default_height

文档

public int getActionBarHeight() {
    int actionBarHeight = 0;
    TypedValue tv = new TypedValue();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv,
                true))
            actionBarHeight = TypedValue.complexToDimensionPixelSize(
                    tv.data, getResources().getDisplayMetrics());
    } else {
        actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,
                getResources().getDisplayMetrics());
    }
    return actionBarHeight;
}

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