Android 如何获得 ActionBar 视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20023483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get ActionBar view?
提问by Emil Adz
I'm using the Showcaselibrary to explain my application feature to
the user. In some point I need to dim the whole ActionBarto present
another feature to the user.
我正在使用该Showcase库向用户解释我的应用程序功能。在某些时候,我需要调暗整体ActionBar以向用户展示另一个功能。
For that I'm using the setAlpha(float num)of the Viewclass. And so for doing that I need to get the actual view instance of my ActionBar
对于我使用setAlpha(float num)的的View类。为此,我需要获取我的实际视图实例ActionBar
By the way, I'm using the support-7-appcompatlibrary that gives ActionBar support for older systems.
顺便说一下,我正在使用support-7-appcompat为旧系统提供 ActionBar 支持的库。
Update
更新
In the meantime I found this option, if you configure a custom view and add it to you ActionBarusing:
同时我找到了这个选项,如果你配置一个自定义视图并将它添加到你ActionBar使用:
getSupportActionBar().setCustomView(v);
Then to get the Viewof the ActionBaryou could do:
然后得到View的ActionBar,你可以这样做:
(View) activity.getSupportActionBar().getCustomView().getParent().getParent()
Is there a simpler or easier way to do that?
有没有更简单或更简单的方法来做到这一点?
回答by idunnololz
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);
}
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 容器使用 id android.R.id.action_bar_container,但这个 id 不是公开的。因此我们使用 getIdentifier() 来检索这个 id,然后剩下的就很简单了。
回答by android developer
I think this solution is more complete, handling both normal Activity and ActionBarActivity.
我认为这个解决方案更完整,处理正常的Activity和ActionBarActivity。
It also handles the case that the actionbar was set using a toolbar, but you need to implement it in the activity you've created:
它还处理使用工具栏设置操作栏的情况,但您需要在您创建的活动中实现它:
public static View getActionBarView(final Activity activity) {
if (activity instanceof IToolbarHolder)
return ((IToolbarHolder) activity).getToolbar();
final String packageName = activity instanceof ActionBarActivity ? activity.getPackageName() : "android";
final int resId = activity.getResources().getIdentifier("action_bar_container", "id", packageName);
final View view = activity.findViewById(resId);
return view;
}
public interface IToolbarHolder {
public android.support.v7.widget.Toolbar getToolbar();
}
回答by Rahmat Rezaei
for support.v7getActionBarView(ById) doesn't work.
对于support.v7getActionBarView(ById) 不起作用。
this returns actionBar Toolbar :
这将返回 actionBar 工具栏:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewGroup actionBar = getActionBar(getWindow().getDecorView());
TextView actionBarTitle = (TextView) actionBar.getChildAt(0);
}
public ViewGroup getActionBar(View view) {
try {
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
if (viewGroup instanceof android.support.v7.widget.Toolbar) {
return viewGroup;
}
for (int i = 0; i < viewGroup.getChildCount(); i++) {
ViewGroup actionBar = getActionBar(viewGroup.getChildAt(i));
if (actionBar != null) {
return actionBar;
}
}
}
} catch (Exception e) {
}
return null;
}
回答by Mohammad Ersan
I made a little fix on @idunnololz code to support ActionBarSherlock
我对@idunnololz 代码做了一些修复以支持 ActionBarSherlock
private View getActionBarView() {
int actionViewResId = 0;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
actionViewResId = getResources().getIdentifier(
"abs__action_bar_container", "id", getPackageName());
} else {
actionViewResId = Resources.getSystem().getIdentifier(
"action_bar_container", "id", "android");
}
if (actionViewResId > 0) {
return this.findViewById(actionViewResId);
}
return null;
}
回答by Jared Rummler
This will get the Toolbar/ActionBarwhen using the native ActionBar, your own Toolbarfrom appcompat, or the native Toolbaron Lollipop:
这将在使用 native 时获得Toolbar/ ,您自己的 appcompat 或Lollipop 上的 native :ActionBarActionBarToolbarToolbar
public static ViewGroup findActionBar(Activity activity) {
int id = activity.getResources().getIdentifier("action_bar", "id", "android");
ViewGroup actionBar = null;
if (id != 0) {
actionBar = (ViewGroup) activity.findViewById(id);
}
if (actionBar == null) {
actionBar = findToolbar((ViewGroup) activity.findViewById(android.R.id.content)
.getRootView());
}
return actionBar;
}
private static ViewGroup findToolbar(ViewGroup viewGroup) {
ViewGroup toolbar = null;
for (int i = 0, len = viewGroup.getChildCount(); i < len; i++) {
View view = viewGroup.getChildAt(i);
if (view.getClass().getName().equals("android.support.v7.widget.Toolbar")
|| view.getClass().getName().equals("android.widget.Toolbar")) {
toolbar = (ViewGroup) view;
} else if (view instanceof ViewGroup) {
toolbar = findToolbar((ViewGroup) view);
}
if (toolbar != null) {
break;
}
}
return toolbar;
}

