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

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

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

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


当前回答

这个助手方法对某些人来说应该会派上用场。示例:int actionBarSize = getThemeAttributeDimensionSize(这,android. r.t r.actionBarSize);

public static int getThemeAttributeDimensionSize(Context context, int attr)
{
    TypedArray a = null;
    try{
        a = context.getTheme().obtainStyledAttributes(new int[] { attr });
        return a.getDimensionPixelSize(0, 0);
    }finally{
        if(a != null){
            a.recycle();
        }
    }
}

其他回答

如果你正在使用Xamarin/ c#,这是你要找的代码:

var styledAttributes = Context.Theme.ObtainStyledAttributes(new[] { Android.Resource.Attribute.ActionBarSize });
var actionBarSize = (int)styledAttributes.GetDimension(0, 0);
styledAttributes.Recycle();

我找到了一个更通用的方法来发现它:

  int[] location = new int[2];
  mainView.getLocationOnScreen(location);
  int toolbarHeight = location[1];

mainView是你布局的根视图。

这个想法基本上是获得主视图的Y位置,因为它被设置在动作栏(或工具栏)的正下方。

好吧,我在谷歌上搜索了几次,找到了这个帖子,所以我认为不仅是对夏洛克的描述,对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

@Anthony回答适用于支持操作栏的设备,以及那些只支持Sherlock操作栏的设备,必须使用以下方法

    TypedValue tv = new TypedValue();
    if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
        actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());

    if(actionBarHeight ==0 && getTheme().resolveAttribute(com.actionbarsherlock.R.attr.actionBarSize, tv, true)){
            actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
    }

   //OR as stated by @Marina.Eariel
   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());
   }

以下是我使用的Kotlin的更新版本,假设phone比Honeycomb更高:

val actionBarHeight = with(TypedValue().also {context.theme.resolveAttribute(android.R.attr.actionBarSize, it, true)}) {
            TypedValue.complexToDimensionPixelSize(this.data, resources.displayMetrics)
        }