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条回答
forever°为你锁心
2楼-- · 2020-01-23 04:36

The Class Summary is usually a good place to start. I think the getHeight() method should suffice.

EDIT:

If you need the width, it should be the width of the screen (right?) and that can be gathered like this.

查看更多
贪生不怕死
3楼-- · 2020-01-23 04:41

@AZ13's answer is good, but as per the Android design guidelines, the ActionBar should be at least 48dp high.

查看更多
▲ chillily
4楼-- · 2020-01-23 04:43

If you're using the compatibility ActionBar from the recent v7 appcompat support package, you can get the height using

@dimen/abc_action_bar_default_height

Documentation

查看更多
虎瘦雄心在
5楼-- · 2020-01-23 04:44

From the de-compiled sources of Android 3.2's framework-res.apk, res/values/styles.xml contains:

<style name="Theme.Holo">
    <!-- ... -->
    <item name="actionBarSize">56.0dip</item>
    <!-- ... -->
</style>

3.0 and 3.1 seem to be the same (at least from AOSP)...

查看更多
时光不老,我们不散
6楼-- · 2020-01-23 04:49

I needed to do replicate these heights properly in a pre-ICS compatibility app and dug into the framework core source. Both answers above are sort of correct.

It basically boils down to using qualifiers. The height is defined by the dimension "action_bar_default_height"

It is defined to 48dip for default. But for -land it is 40dip and for sw600dp it is 56dip.

查看更多
\"骚年 ilove
7楼-- · 2020-01-23 04:51

I did in this way for myself, this helper method should come in handy for someone:

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;
}
查看更多
登录 后发表回答