How to get ActionBar view?

前端 未结 5 1280
野性不改
野性不改 2020-11-28 11:10

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

相关标签:
5条回答
  • 2020-11-28 11:36

    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;
    }
    
    0 讨论(0)
  • 2020-11-28 11:38

    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();
    }
    
    0 讨论(0)
  • 2020-11-28 11:47

    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;
    }
    
    0 讨论(0)
  • 2020-11-28 11:50

    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;
    }
    
    0 讨论(0)
  • 2020-11-28 11:51

    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.

    0 讨论(0)
提交回复
热议问题