Evenly spaced menu items on Toolbar

前端 未结 8 683
心在旅途
心在旅途 2020-12-01 02:09

So I\'ve been trying to implement android.support.v7.widget.Toolbar in my Activity and to make it look similar to the previously supported split ActionBar.

相关标签:
8条回答
  • 2020-12-01 03:02

    This solution take the best from each of above solutions , Thanks to inner_class7 ,Kuffs & MrEngineer13.
    This solution evenly distribute the menu items and shows the text .

    public class EvenlyDistributedToolbar extends android.support.v7.widget.Toolbar {

        private View actionMenuView;
        public EvenlyDistributedToolbar(Context context) {
            super(context);
            setContentInsetsAbsolute(0, 0);
        }
    
        public EvenlyDistributedToolbar(Context context, AttributeSet attrs) {
            super(context, attrs);
            setContentInsetsAbsolute(0, 0);
        }
    
        public EvenlyDistributedToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            setContentInsetsAbsolute(0, 0);
        }
    
        @Override
        public void addView(View child, ViewGroup.LayoutParams params) {
            if (child instanceof ActionMenuView) {
                actionMenuView  = child ;
                params.width = LayoutParams.MATCH_PARENT;
    
                ((ViewGroup)actionMenuView).setOnHierarchyChangeListener(new OnHierarchyChangeListener() {
    
                    @Override 
                    public void onChildViewRemoved(View parent, View child) {
    
                    } 
    
                    @Override 
                    public void onChildViewAdded(View parent, View child) {
                        if (child instanceof ActionMenuItemView) {
                            //Show the menu item text as well as the the icon
                            ActionMenuItemView actionMenuItemView = (ActionMenuItemView) child;
                            // set the layout parameters on each View
                            actionMenuItemView.setExpandedFormat(true);
                            Drawable[] arr = actionMenuItemView.getCompoundDrawables();
    
                            if (arr != null && arr.length == 4 && arr[0] != null) {
                                actionMenuItemView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
                            }
                            else if (arr != null && arr.length == 4 && arr[2] != null) {
                                actionMenuItemView.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
                            }
                            actionMenuItemView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
                            actionMenuItemView.setOnLongClickListener(null);
    
                        }
                    } 
                }); 
            }
            super.addView(child, params);
        }
    
    
        /**
         * Show All items, call after the menu inflated
         */
        public void showAll() {
            Menu menu = getMenu();
    
            int size = menu.size();
            for (int i = 0; i < size; i++) {
                MenuItem item = menu.getItem(i);
                // check if app:showAsAction = "ifRoom"
                if (((MenuItemImpl) item).requestsActionButton()) {
                    item.setShowAsAction(SupportMenuItem.SHOW_AS_ACTION_ALWAYS);
                }
            }
        }
    
    
    }
    
            <com.util.EvenlyDistributedToolbar
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />
    
    0 讨论(0)
  • 2020-12-01 03:07

    If you create your menu programmatically rather than by inflating from resources, you can do this:

    Use the SplitToolbar as mentioned in another answer. Get a reference to the toolbar using FindViewById as normal. If the toolbar does not exist in the layout, the menu functions as a normal non-split version.

    import android.content.Context;
    import android.support.v7.widget.ActionMenuView;
    import android.support.v7.widget.Toolbar;
    import android.util.AttributeSet;
    import android.view.View;
    import android.view.ViewGroup;
    
    public class SplitToolbar extends Toolbar {
        public SplitToolbar(Context context) {
            super(context);
        }
    
        public SplitToolbar(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public SplitToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        public void addView(View child, ViewGroup.LayoutParams params) {
            if (child instanceof ActionMenuView) {
                params.width = LayoutParams.MATCH_PARENT;
            }
            super.addView(child, params);
        }
    }
    

    Then in your menu creation code do the following.

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
    
        if (toolbar != null) {
           toolbar.setContentInsetsAbsolute(0,0);
            menu = toolbar.getMenu();
            toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem menuItem) {
                    // Call back to the original menu code to handle menu clicks
                    return onOptionsItemSelected(menuItem);
                }
            });
        }
    
        // Now build your menu as normal
        menu.clear();
    
        MenuItem b = menu.add(0, WHATEVER, 0, R.string.WHATEVER);
                 b.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
                 b.setIcon(R.drawable.ic_menu_encrypt);
        // End of normal menu code
    
        // Now set the button options.
        if (toolbar != null) {
            int size = menu.size();
            for (int i = 0; i < size; i++) {
                MenuItem item = menu.getItem(i);
                // check if app:showAsAction = "ifRoom"
                if (((MenuItemImpl) item).requestsActionButton()) {
                    item.setShowAsAction(SupportMenuItem.SHOW_AS_ACTION_ALWAYS);
                }
            }
        }
        Return true; 
    }
    
    0 讨论(0)
提交回复
热议问题