我需要知道ActionBar的确切大小(以像素为单位),以便应用正确的背景图像。
答案 0 :(得分:522)
要以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 :(得分:54)
从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)......
答案 2 :(得分:43)
要获取Actionbar的实际高度,您必须在运行时解析属性actionBarSize
。
TypedValue tv = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
int actionBarHeight = getResources().getDimensionPixelSize(tv.resourceId);
答案 3 :(得分:33)
其中一个Honeycomb样本是指?android:attr/actionBarSize
答案 4 :(得分:20)
我需要在pre-ICS兼容性应用程序中正确复制这些高度并挖掘framework core source。上面的两个答案都是正确的。
它基本上归结为使用限定符。高度由维度“action_bar_default_height”
定义默认情况下定义为48dip。但是对于-land来说它是40dip而对于sw600dp则是56dip。
答案 5 :(得分:17)
如果您使用的是最近的v7 appcompat支持包中的兼容性ActionBar,则可以使用
获取高度@dimen/abc_action_bar_default_height
答案 6 :(得分:16)
使用新的v7 support library(21.0.0),R.dimen
中的名称已更改为@dimen/abc_action_bar_default_height_material。
从早期版本的支持库升级时,您应该将该值用作操作栏的高度
答案 7 :(得分:9)
如果您使用的是ActionBarSherlock,则可以使用
获得高度@dimen/abs__action_bar_default_height
答案 8 :(得分:4)
@ AZ13的答案很好,但根据Android design guidelines,ActionBar应为at least 48dp high。
答案 9 :(得分: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;
}
答案 10 :(得分:0)
答案 11 :(得分:0)
在我的Galaxy S4上有&gt; 441dpi&gt; 1080 x 1920&gt; 使用getResources()获取Actionbar高度.getDimensionPixelSize我有144像素。
使用公式px = dp x(dpi / 160),我使用441dpi,而我的设备位于
在类别480dpi。所以把它确认结果。
答案 12 :(得分: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;
}