我试图获得的高度的动作栏(使用夏洛克)每次活动被创建(特别处理配置变化的旋转,其中动作栏的高度可能会改变)。

为此,我使用方法ActionBar. getheight(),它只在显示ActionBar时起作用。

当第一个活动第一次创建时,我可以在onCreateOptionsMenu回调中调用getHeight()。但是之后不会调用此方法。

所以我的问题是,我什么时候可以调用getHeight(),并确保它不返回0? 或者如果不可能,我如何设置动作栏的高度?


当前回答

要设置动作栏的高度,你可以创建一个新的主题,就像这样:

<?xml version="1.0" encoding="utf-8"?>
<resources>   
    <style name="Theme.FixedSize" parent="Theme.Sherlock.Light.DarkActionBar">
        <item name="actionBarSize">48dip</item>
        <item name="android:actionBarSize">48dip</item> 
    </style> 
 </resources>

并将此主题设置为您的活动:

android:theme="@style/Theme.FixedSize"

其他回答

动作栏的高度根据应用的主题而不同。 如果你正在使用AppCompatActivity,高度在大多数情况下与正常的Activity不同。

如果你正在使用AppCompatActivity,你应该使用r.t r. actionbarsize而不是android. r.t r. actionbarsize

public static int getActionBarHeight(Activity activity) {
        TypedValue typedValue = new TypedValue();

        int attributeResourceId = android.R.attr.actionBarSize;
        if (activity instanceof AppCompatActivity) {
            attributeResourceId = R.attr.actionBarSize;
        }

        if (activity.getTheme().resolveAttribute(attributeResourceId, typedValue, true)) {
            return TypedValue.complexToDimensionPixelSize(typedValue.data, activity.getResources().getDisplayMetrics());
        }

        return (int) Math.floor(activity.getResources()
                .getDimension(R.dimen.my_default_value));
    }

我认为最安全的方法是:

private int getActionBarHeight() {
    int actionBarHeight = getSupportActionBar().getHeight();
    if (actionBarHeight != 0)
        return actionBarHeight;
    final 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 if (getTheme().resolveAttribute(com.actionbarsherlock.R.attr.actionBarSize, tv, true))
        actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
    return actionBarHeight;
}

一个现成的方法来完成最流行的答案:

public static int getActionBarHeight(
  Activity activity) {

  int actionBarHeight = 0;
  TypedValue typedValue = new TypedValue();

  try {

    if (activity
      .getTheme()
      .resolveAttribute(
        android.R.attr.actionBarSize,
        typedValue,
        true)) {

      actionBarHeight =
        TypedValue.complexToDimensionPixelSize(
          typedValue.data,
          activity
            .getResources()
            .getDisplayMetrics());
    }

  } catch (Exception ignore) {
  }

  return actionBarHeight;
}

在XML中,你应该使用这个属性:

android:paddingTop="?android:attr/actionBarSize"

好吧,我在谷歌上搜索了几次,找到了这个帖子,所以我认为不仅是对夏洛克的描述,对appcompat的描述也很好:

操作栏高度使用appCompat

非常类似于@Anthony描述,你可以用下面的代码找到它(我刚刚改变了资源id):

TypedValue tv = new TypedValue();

if (getActivity().getTheme().resolveAttribute(R.attr.actionBarSize, tv, true))
{
    int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}

三明治前问题

我需要操作栏的高度,因为在pre ICE_CREAM_SANDWITCH设备上,我的视图是重叠的操作栏。我试着设置android:layout_marginTop="?android: attr / actionBarSize”,android: layout_marginTop = "吗?attr/actionBarSize”,设置重叠关闭查看/动作栏,甚至固定的动作栏高度与自定义主题,但没有工作。它看起来是这样的:

不幸的是,我发现上述方法,我只得到一半的高度(我假设选项栏没有到位),所以要完全解决这个问题,我需要双重行动栏的高度,一切似乎都ok:

如果有人知道更好的解决方案(比加倍actionBarHeight),我很高兴让我知道,因为我来自iOS开发,我发现大多数Android视图的东西相当混乱:)

问候,

hris.to