What is the size of ActionBar in pixels?

2020-01-23 04:00发布

I need to know the exact size of ActionBar in pixels so to apply correct background image.

13条回答
2楼-- · 2020-01-23 04:28

With the new v7 support library (21.0.0) the name in R.dimen has changed to @dimen/abc_action_bar_default_height_material.

When upgrading from a previous version of the support lib you should therefore use that value as the actionbar's height

查看更多
叛逆
3楼-- · 2020-01-23 04:29

To retrieve the height of the ActionBar in XML, just use

?android:attr/actionBarSize

or if you're an ActionBarSherlock or AppCompat user, use this

?attr/actionBarSize

If you need this value at runtime, use this

final TypedArray styledAttributes = getContext().getTheme().obtainStyledAttributes(
                    new int[] { android.R.attr.actionBarSize });
mActionBarSize = (int) styledAttributes.getDimension(0, 0);
styledAttributes.recycle();

If you need to understand where this is defined:

  1. The attribute name itself is defined in the platform's /res/values/attrs.xml
  2. The platform's themes.xml picks this attribute and assigns a value to it.
  3. The value assigned in step 2 depends on different device sizes, which are defined in various dimens.xml files in the platform, ie. core/res/res/values-sw600dp/dimens.xml
查看更多
做个烂人
4楼-- · 2020-01-23 04:30

To get the actual height of the Actionbar, you have to resolve the attribute actionBarSize at runtime.

TypedValue tv = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
int actionBarHeight = getResources().getDimensionPixelSize(tv.resourceId);
查看更多
戒情不戒烟
5楼-- · 2020-01-23 04:31

One of the Honeycomb samples refers to ?android:attr/actionBarSize

查看更多
在下西门庆
6楼-- · 2020-01-23 04:35

On my Galaxy S4 having > 441dpi > 1080 x 1920 > Getting Actionbar height with getResources().getDimensionPixelSize I got 144 pixels.

Using formula px = dp x (dpi/160), I was using 441dpi, whereas my device lies
in the category 480dpi. so putting that confirms the result.

查看更多
我只想做你的唯一
7楼-- · 2020-01-23 04:36

If you are using ActionBarSherlock, you can get the height with

@dimen/abs__action_bar_default_height
查看更多
登录 后发表回答