279

我需要知道 ActionBar 的确切大小(以像素为单位),以便应用正确的背景图像。

4

14 回答 14

593

要在 XML 中检索 ActionBar 的高度,只需使用

?android:attr/actionBarSize

或者如果您是 ActionBarSherlock 或 AppCompat 用户,请使用此

?attr/actionBarSize

如果您在运行时需要此值,请使用此

final TypedArray styledAttributes = getContext().getTheme().obtainStyledAttributes(
                    new int[] { android.R.attr.actionBarSize });
mActionBarSize = (int) styledAttributes.getDimension(0, 0);
styledAttributes.recycle();

如果您需要了解这是在哪里定义的:

  1. 属性名称本身在平台的/res/values/attrs.xml中定义
  2. 平台的themes.xml选择此属性并为其分配一个值。
  3. 步骤2中分配的值取决于不同的设备大小,在平台的各种dimens.xml文件中定义,即。核心/res/res/values-sw600dp/dimens.xml
于 2011-08-23T20:22:56.317 回答
60

从 Android 3.2 的反编译源中framework-res.apkres/values/styles.xml包含:

<style name="Theme.Holo">
    <!-- ... -->
    <item name="actionBarSize">56.0dip</item>
    <!-- ... -->
</style>

3.0 和 3.1 似乎是一样的(至少来自 AOSP)...

于 2011-08-24T20:05:48.180 回答
48

要获取 Actionbar 的实际高度,您必须actionBarSize在运行时解析该属性。

TypedValue tv = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
int actionBarHeight = getResources().getDimensionPixelSize(tv.resourceId);
于 2012-10-23T09:04:25.483 回答
35

蜂窝样品之一是指?android:attr/actionBarSize

于 2011-09-26T03:42:05.720 回答
21

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

它基本上归结为使用限定符。高度由维度“action_bar_default_height”定义

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

于 2012-01-27T20:58:32.777 回答
17

如果您使用最新的 v7 appcompat 支持包中的兼容性 ActionBar,您可以使用以下方法获取高度

@dimen/abc_action_bar_default_height

文档

于 2013-07-31T00:31:31.627 回答
16

使用新的v7 支持库(21.0.0) 中的名称R.dimen已更改为@dimen/abc_action_bar_default_height_ material

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

于 2014-10-21T14:26:31.307 回答
9

如果您使用的是 ActionBarSherlock,您可以使用

@dimen/abs__action_bar_default_height
于 2013-03-20T11:06:03.080 回答
5

@AZ13 的回答很好,但根据Android 设计指南,ActionBar至少应为48dp 高。

于 2012-06-26T04:05:02.640 回答
1
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;
}
于 2020-01-22T04:06:47.410 回答
1

Kotlin 中接受的答案:

val Context.actionBarSize
    get() = theme.obtainStyledAttributes(intArrayOf(android.R.attr.actionBarSize))
        .let { attrs -> attrs.getDimension(0, 0F).toInt().also { attrs.recycle() } }

用法 :

val size = actionBarSize                    // Inside Activity
val size = requireContext().actionBarSize   // Inside Fragment
val size = anyView.context.actionBarSize    // Inside RecyclerView ViewHolder
于 2021-08-26T14:39:13.793 回答
0

类摘要通常是一个很好的起点。我认为 getHeight() 方法就足够了。

编辑:

如果您需要宽度,它应该是屏幕的宽度(对吗?),可以像这样收集。

于 2011-08-23T18:35:21.377 回答
0

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

使用公式 px = dp x (dpi/160),我使用的是 441dpi,而我的设备属于 480dpi
类别。所以把这证实了结果。

于 2014-09-06T11:28:07.743 回答
0

我为自己这样做了,这个辅助方法应该对某人派上用场:

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;
}
于 2016-01-11T06:04:07.913 回答