Evenly spaced menu items on Toolbar

前端 未结 8 776
心在旅途
心在旅途 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 02:45

    UPDATE

    Google now has very similar functionality inflated the normal ways menus inflate using a new Widget call BottomNavigationView

    -- Original Answer --

    Guys this took me some time to figure out and here you go its a little bit of a heavy operation but it works.

    I use this on a Toolbar to display across the bottom of the screen like the old SplitActionBar...

    BEHOLD the evenly distributed MenuItems across your Toolbar

    I would not recommend using more than 5 or 6 items, it may get a little crowded...

    /**
     * This method will take however many items you have in your  
     * menu/menu_main.xml and distribute them across your devices screen
     * evenly using a Toolbar. Enjoy!!
     */
    public void setupEvenlyDistributedToolbar(){
        // Use Display metrics to get Screen Dimensions
        Display display = getWindowManager().getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);
    
        // Toolbar
        mToolbar = (Toolbar) findViewById(R.id.navigationToolbar);
        // Inflate your menu
        mToolbar.inflateMenu(R.menu.menu_bottom);
    
        // Add 10 spacing on either side of the toolbar
        mToolbar.setContentInsetsAbsolute(10, 10);
    
        // Get the ChildCount of your Toolbar, this should only be 1
        int childCount = mToolbar.getChildCount();
        // Get the Screen Width in pixels
        int screenWidth = metrics.widthPixels;
    
        // Create the Toolbar Params based on the screenWidth
        Toolbar.LayoutParams toolbarParams = new Toolbar.LayoutParams(screenWidth, LayoutParams.WRAP_CONTENT);
    
        // Loop through the child Items
        for(int i = 0; i < childCount; i++){
            // Get the item at the current index
            View childView = mToolbar.getChildAt(i);
            // If its a ViewGroup
            if(childView instanceof ViewGroup){
                // Set its layout params
                childView.setLayoutParams(toolbarParams);
                // Get the child count of this view group, and compute the item widths based on this count & screen size
                int innerChildCount = ((ViewGroup) childView).getChildCount();
                int itemWidth  = (screenWidth / innerChildCount);               
                // Create layout params for the ActionMenuView
                ActionMenuView.LayoutParams params = new ActionMenuView.LayoutParams(itemWidth, LayoutParams.WRAP_CONTENT);
                // Loop through the children
                for(int j = 0; j < innerChildCount; j++){
                    View grandChild = ((ViewGroup) childView).getChildAt(j);
                    if(grandChild instanceof ActionMenuItemView){
                        // set the layout parameters on each View
                        grandChild.setLayoutParams(params);
                    }
                }
            }
        }
    }
    

提交回复
热议问题