How to get ActionBar view?

2020-01-25 07:43发布

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.

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

By the way, I'm using the support-7-appcompat library that gives ActionBar support for older systems.

Update

In the meantime I found this option, if you configure a custom view and add it to you ActionBar using:

getSupportActionBar().setCustomView(v);

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?

5条回答
姐就是有狂的资本
2楼-- · 2020-01-25 07:58

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.

查看更多
姐就是有狂的资本
3楼-- · 2020-01-25 08:06

I think this solution is more complete, handling both normal Activity and 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();
}
查看更多
对你真心纯属浪费
4楼-- · 2020-01-25 08:10

This will get the Toolbar/ActionBar when using the native ActionBar, your own Toolbar from appcompat, or the native Toolbar on Lollipop:

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;
}
查看更多
混吃等死
5楼-- · 2020-01-25 08:13

I made a little fix on @idunnololz code to support 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;
}
查看更多
狗以群分
6楼-- · 2020-01-25 08:15

for support.v7 getActionBarView(ById) doesn't work.

this returns actionBar Toolbar :

@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;
}
查看更多
登录 后发表回答