Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

android - How to center action menu on toolbar

I have an application with a split action bar loading an action menu.

I changed the actionbar for the new toolbar and replaced the split actionbar by an other toolbar used in standalone mode :

Toolbar toolbarBottom = (Toolbar) findViewById(R.id.toolbarBottom);
toolbarBottom.inflateMenu(R.menu.ab_one_cam);

As specified in the documentation the action menu is pin to the right of the toolbar : enter image description here

But i would like the icons to be centered on the toolbar , like it was on the split actionbar : enter image description here

How can i make the action menu take all the available space on the toolbar ?

The toolbar is dedicated to this menu , nothing else will be added on it.

Answer

The accepted answer's link lead to a split toolbar. If like me you have very simple need this code is good enough :

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);
    }
}

Credit goes to : https://gist.github.com/dodgex/7bc81fd2cbb70a8d5117

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In this case, Chris Banes recommends to use ActionMenuView instead of Toolbar (see the link below, reply #6). Besides that, in this link you can find a solution where the guy subclassed Toolbar in order to the split works right.

https://code.google.com/p/android/issues/detail?id=77632#c2

Hope it helps you!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...