如何获得 ActionBar 视图? [英] How to get ActionBar view?
问题描述
我正在使用 Showcase
库来解释我的应用程序功能用户.在某些时候,我需要将整个 ActionBar
调暗以呈现给用户的另一个功能.
I'm using the Showcase
library to explain my application feature to
the user. In some point I need to dim the whole ActionBar
to present
another feature to the user.
为此,我使用了 View
类的 setAlpha(float num)
.为此,我需要获取 ActionBar
For that I'm using the setAlpha(float num)
of the View
class. And so for doing that I need to get the actual view instance of my ActionBar
顺便说一下,我正在使用 support-7-appcompat
库,它为旧系统提供 ActionBar 支持.
By the way, I'm using the support-7-appcompat
library that gives ActionBar support for older systems.
同时我发现了这个选项,如果你配置一个自定义视图并将它添加到你 ActionBar
使用:
In the meantime I found this option, if you configure a custom view and add it to you ActionBar
using:
getSupportActionBar().setCustomView(v);
然后要获得 ActionBar
的 View
你可以这样做:
Then to get the View
of the ActionBar
you could do:
(View) activity.getSupportActionBar().getCustomView().getParent().getParent()
有没有更简单或更容易的方法来做到这一点?
Is there a simpler or easier way to do that?
推荐答案
是的.您实际上可以使用此函数获取视图:
Yep. You can actually get the view by using this function:
public View getActionBarView() {
Window window = getWindow();
View v = window.getDecorView();
int resId = getResources().getIdentifier("action_bar_container", "id", "android");
return v.findViewById(resId);
}
几乎是这样的,actionbar 容器使用 id android.R.id.action_bar_container
,但是这个 id 不是公开的.因此我们使用 getIdentifier() 来检索这个 id,然后剩下的就很简单了.
Pretty much the way this works is that the actionbar container uses the id android.R.id.action_bar_container
, but this id is not public. Therefore we use getIdentifier() to retrieve this id and then the rest is simple.
这篇关于如何获得 ActionBar 视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!