我需要知道动作栏的像素大小,以便应用正确的背景图像。
要在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步中分配的值取决于不同的设备大小,这些设备大小在平台中的各种dimensions .xml文件中定义,例如。核心/ res / res / values-sw600dp / dimens.xml
在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来看)…
我需要在一个pre-ICS兼容应用程序中正确复制这些高度,并深入研究框架核心源代码。以上两个答案都是正确的。
它基本上可以归结为使用限定词。高度由维度"action_bar_default_height"定义
默认定义为48dip。但是对于-land是40dip,对于sw600dp是56dip。
为了获得动作栏的实际高度,你必须在运行时解析actionBarSize属性。
TypedValue tv = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
int actionBarHeight = getResources().getDimensionPixelSize(tv.resourceId);
如果你正在使用最近的v7 appcompat支持包中的兼容ActionBar,你可以使用
@dimen/abc_action_bar_default_height
文档
在我的Galaxy S4上有> 441dpi > 1080 x 1920 > 使用getResources()获取动作栏高度。我得到144像素。
使用公式px = dp x (dpi/160),我使用的是441dpi,而我的设备位于 在类别480dpi。这样就证实了这个结果。
在新的v7支持库(21.0.0)中,R.dimen中的名称已更改为@dimen/ abc_action_bar_default_highight_material。
当从以前版本的支持库升级时,您应该使用该值作为动作栏的高度
我这样做是为了自己,这个帮手方法应该对其他人有用:
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;
}
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;
}
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
推荐文章
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?
- adb找不到我的设备/手机(MacOS X)
- 如何在新的材质主题中改变背面箭头的颜色?
- androidviewpager与底部点
- 相同的导航抽屉在不同的活动
- 如何从视图中获得托管活动?
- 单一的TextView与多种颜色的文本
- 如何在非活动类(LocationManager)中使用getSystemService ?
- 在清单中注册应用程序类?
- Android:从数组中编程创建旋转器
- Android命令行工具sdkmanager总是显示:警告:无法创建设置
- 如何设置RecyclerView应用程序:layoutManager=""从XML?
- 在没有开发服务器的情况下在设备上构建和安装unsigned apk ?