首页 文章

ActionBar的大小(以像素为单位)是多少?

提问于
浏览
232

我需要知道ActionBar的确切大小(以像素为单位),以便应用正确的背景图像 .

12 回答

  • 42

    从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)......

  • 9

    要获取Actionbar的实际高度,必须在运行时解析属性 actionBarSize .

    TypedValue tv = new TypedValue();
    context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
    int actionBarHeight = getResources().getDimensionPixelSize(tv.resourceId);
    
  • 50

    其中一个蜂窝样品指的是 ?android:attr/actionBarSize

  • 0

    要在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中分配的值取决于不同的设备大小,这些大小在平台的various dimens.xml files中定义,即 . 芯/ RES / RES /值-sw600dp / dimens.xml

  • 0

    我需要在pre-ICS兼容性应用程序中正确复制这些高度并挖掘framework core source . 以上两个答案都是正确的 .

    它基本上归结为使用限定符 . 高度由维度“action_bar_default_height”定义

    它默认定义为48dip . 但是对于-land来说它是40dip而对于sw600dp它是56dip .

  • 20

    如果您使用的是最近的v7 appcompat支持包中的兼容性ActionBar,则可以使用高度获取高度

    @dimen/abc_action_bar_default_height
    

    Documentation

  • 0

    使用新的v7 support library(21.0.0), R.dimen 中的名称已更改为@dimen/abc_action_bar_default_height_material .

    从早期版本的支持库升级时,您应该使用该值作为操作栏的高度

  • 500

    如果您使用的是ActionBarSherlock,则可以获得高度

    @dimen/abs__action_bar_default_height
    
  • 16

    @ AZ13的答案很好,但根据Android design guidelines,ActionBar应该是at least 48dp high .

  • 17

    Class Summary通常是一个很好的起点 . 我认为getHeight()方法应该足够了 .

    编辑:

    如果你需要宽度,它应该是屏幕的宽度(对吗?),并且可以收集like this .

  • 32

    在我的Galaxy S4上> 441dpi> 1080 x 1920>使用getResources()获取Actionbar高度.getDimensionPixelSize我得到144像素 .

    使用公式px = dp x(dpi / 160),我使用441dpi,而我的设备是谎言
    在类别480dpi . 所以把它确认结果 .

  • 4

    我这样对待自己,这个辅助方法对某些人来说应该派上用场:

    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;
    }
    

相关问题