android - ActionBar的大小(以像素为单位)是多少?
我需要知道ActionBar的确切大小(以像素为单位),以便应用正确的背景图像。
13个解决方案
499 votes
要在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();
如果您需要了解定义的位置:
属性名称本身在平台的/res/values/attrs.xml中定义
平台的themes.xml选择此属性并为其分配值。
步骤2中分配的值取决于不同的设备大小,这些大小在平台中的各种dimens.xml文件中定义,即。芯/ RES / RES /值-sw600dp/ dimens.xml
AZ13 answered 2019-02-21T08:18:18Z
50 votes
从Android 3.2的framework-res.apk的解编译来源,res/values/styles.xml包含:
56.0dip
3.0和3.1似乎是相同的(至少来自AOSP)......
Jake Wharton answered 2019-02-21T08:18:53Z
41 votes
要获取Actionbar的实际高度,必须在运行时解析属性actionBarSize。
TypedValue tv = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
int actionBarHeight = getResources().getDimensionPixelSize(tv.resourceId);
David Boho answered 2019-02-21T08:19:27Z
32 votes
Honeycomb样品之一是指?android:attr/actionBarSize
Vikram Bodicherla answered 2019-02-21T08:20:00Z
20 votes
我需要在pre-ICS兼容性应用程序中正确复制这些高度并挖掘到框架核心源。 以上两个答案都是正确的。
它基本上归结为使用限定符。 高度由维度“action_bar_default_height”定义
它默认定义为48dip。 但是对于-land来说它是40dip而对于sw600dp它是56dip。
Manfred Moser answered 2019-02-21T08:20:43Z
17 votes
如果您使用的是最近的v7 appcompat支持包中的兼容性ActionBar,则可以使用高度获取高度
@dimen/abc_action_bar_default_height
文档
flawedmatrix answered 2019-02-21T08:21:11Z
16 votes
使用新的v7支持库(21.0.0),R.dimen中的名称已更改为@ dimen / abc_action_bar_default_height_material。
从早期版本的支持库升级时,您应该使用该值作为操作栏的高度
MrMaffen answered 2019-02-21T08:21:44Z
9 votes
如果您使用的是ActionBarSherlock,则可以获得高度
@dimen/abs__action_bar_default_height
JesperB answered 2019-02-21T08:22:09Z
4 votes
@ AZ13的答案很好,但根据Android的设计指南,ActionBar应该至少高48dp。
aaronsnoswell answered 2019-02-21T08:22:36Z
0 votes
课程摘要通常是一个很好的起点。 我认为getHeight()方法应该足够了。
编辑:
如果您需要宽度,它应该是屏幕的宽度(对吗?),并且可以像这样收集。
Matt answered 2019-02-21T08:23:11Z
0 votes
在我的Galaxy S4上有> 441dpi> 1080 x 1920>使用getResources()获取Actionbar高度.getDimensionPixelSize我有144像素。
使用公式px = dp x(dpi / 160),我使用441dpi,而我的设备是谎言
在类别480dpi。 所以把它确认结果。
Muhammad answered 2019-02-21T08:23:52Z
0 votes
我这样对待自己,这个辅助方法对某些人来说应该派上用场:
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;
}
Min2 answered 2019-02-21T08:24:18Z
-1 votes
筛选比例为:8.3:100
例如:如果您的屏幕是1024像素,那么84将是您的操作栏高度。
Tushar Pandey answered 2019-02-21T08:24:50Z