如何获取 ActionBar 的高度? [英] How to get the ActionBar height?

查看:23
本文介绍了如何获取 ActionBar 的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在每次创建活动时获取 ActionBar(使用 Sherlock)的高度(特别是为了处理 ActionBar 高度可能改变的旋转配置更改).

I am trying to get the height of the ActionBar (using Sherlock) every time an activity is created (specially to handle configuration changes on rotation where the ActionBar height might change).

为此,我使用了 ActionBar.getHeight() 方法,该方法仅在显示 ActionBar 时有效.

For this I use the method ActionBar.getHeight() which works only when the ActionBar is shown.

当第一次创建第一个活动时,我可以在 onCreateOptionsMenu 回调中调用 getHeight().但是这个方法没有被调用.

When the first activity is created for the first time, I can call getHeight() in the onCreateOptionsMenu callback. But this method is not called after.

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

So my question is when can I call getHeight() and be assured that it doesn't return 0? Or if it is not possible, how can I set the height of the ActionBar ?

推荐答案

虽然@birdy 的答案是一个选项,如果您想显式控制 ActionBar 的大小,有一种方法可以在不锁定我在中找到的大小的情况下将其拉起来支持文档.这有点尴尬,但它对我有用.您需要一个上下文,此示例在 Activity 中有效.

While @birdy's answer is an option if you want to explicitly control the ActionBar size, there is a way to pull it up without locking the size that I found in support documentation. It's a little awkward but it's worked for me. You'll need a context, this example would be valid in an Activity.

// Calculate ActionBar height
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
    int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}

科特林:

val tv = TypedValue()
if (requireActivity().theme.resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
    val actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, resources.displayMetrics)
}

这篇关于如何获取 ActionBar 的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆