【发布时间】:2011-11-02 05:02:21
【问题描述】:
我需要知道 ActionBar 的确切大小(以像素为单位),以便应用正确的背景图像。
【问题讨论】:
我需要知道 ActionBar 的确切大小(以像素为单位),以便应用正确的背景图像。
【问题讨论】:
要在 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();
如果您需要了解这是在哪里定义的:
【讨论】:
@dimen/abc_action_bar_default_height(ActionBarComapt)并且它工作(在 mdpi 设备上)。但是试图在三星 Galaxy SIII 上获得这个值却给我带来了错误的值。这是因为在横向模式下,values-xlarge(不知何故)比values-land 更受欢迎。引用属性就像一个魅力。
android.R.attr.actionBarSize 将在 3.0 之前的设备上解析为 0 大小。因此,当使用ActionBarCompat 时,人们会坚持使用android.support.v7.appcompat.R.attr.actionBarSize。
来自 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)...
【讨论】:
要获取 Actionbar 的实际高度,您必须在运行时解析属性 actionBarSize。
TypedValue tv = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
int actionBarHeight = getResources().getDimensionPixelSize(tv.resourceId);
【讨论】:
其中一个 Honeycomb 样本引用了?android:attr/actionBarSize
【讨论】:
?attr/actionBarSize(注意缺少 android 命名空间),适用于所有 API 级别。
我需要在 ICS 之前的兼容性应用程序中正确复制这些高度,并深入了解 framework core source。上面两个答案都是正确的。
它基本上归结为使用限定符。高度由维度“action_bar_default_height”定义
默认定义为 48dip。但 -land 为 40dip,sw600dp 为 56dip。
【讨论】:
如果您使用的是最新 v7 appcompat 支持包中的兼容性 ActionBar,您可以使用以下方法获取高度
@dimen/abc_action_bar_default_height
【讨论】:
使用新的 v7 support library (21.0.0),R.dimen 中的名称已更改为 @dimen/abc_action_bar_default_height_material。
从以前版本的支持库升级时,您应该使用该值作为操作栏的高度
【讨论】:
ActionBar,这肯定会胜过?attr/actionBarSize。
如果你使用的是 ActionBarSherlock,你可以用
@dimen/abs__action_bar_default_height
【讨论】:
abs__-前缀资源。
@AZ13 的回答很好,但是按照Android design guidelines,ActionBar 应该是at least 48dp high。
【讨论】:
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;
}
【讨论】:
【讨论】:
在我的 Galaxy S4 上,分辨率 > 441dpi > 1080 x 1920 > 使用 getResources().getDimensionPixelSize 获取 Actionbar 高度我得到了 144 像素。
使用公式 px = dp x (dpi/160),我使用的是 441dpi,而我的设备位于
在 480dpi 类别中。所以把这证实了结果。
【讨论】:
我自己就是这样做的,这个辅助方法应该对某人有用:
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;
}
【讨论】:
在 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
【讨论】: