ActionBar 的大小是多少(以像素为单位)?

问题描述 投票:0回答:15

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

android android-actionbar
15个回答
607
投票

要在 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

64
投票

从Android 3.2的

framework-res.apk
反编译源码中,
res/values/styles.xml
包含:

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

3.0 和 3.1 似乎是相同的(至少从 AOSP 来看)...


48
投票

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

actionBarSize

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

35
投票

其中一个蜂窝样品指的是

?android:attr/actionBarSize


21
投票

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

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

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


17
投票

如果您使用最近 v7 appcompat 支持包中的兼容性 ActionBar,您可以使用以下命令获取高度:

@dimen/abc_action_bar_default_height

文档


16
投票
使用新的

v7 支持库 (21.0.0),R.dimen

 中的名称已更改为 
@dimen/abc_action_bar_default_height_material

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


9
投票
如果您使用 ActionBarSherlock,您可以使用

获取高度

@dimen/abs__action_bar_default_height
    

5
投票
@AZ13 的答案很好,但是根据

Android 设计指南,ActionBar 应该是 至少 48dp 高


2
投票
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
    

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; }
    

0
投票

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

编辑:

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


0
投票

使用公式 px = dp x (dpi/160),我使用的是 441dpi,而我的设备位于

在 480dpi 类别中。这样就证实了结果。



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; }



0
投票

<item name="actionBarSize">@dimen/abc_action_bar_default_height_material</item> // values.xml <dimen name="abc_action_bar_default_height_material">56dp</dimen> //values-land.xml <dimen name="abc_action_bar_default_height_material">48dp</dimen> //values-sw600dp-v13.xml <dimen name="abc_action_bar_default_height_material">64dp</dimen>

© www.soinside.com 2019 - 2024. All rights reserved.