如何中心工具栏上的操作菜单 [英] How to center action menu on toolbar

查看:101
本文介绍了如何中心工具栏上的操作菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个拆分操作栏加载应用程序一个操作菜单。

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 :

但我想的图标为中心在工具栏上,就像是在分割动作条:

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

我怎样才能使操作菜单拿工具栏上的所有可用空间?

工具栏是专门为这个菜单,没有别的将它添加。

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

答案

接受的答案的链接导致分裂的工具栏。如果你像我一样有非常简单的需要这个code是不够好:

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

幸得: https://gist.github.com/dodgex/7bc81fd2cbb70a8d5117

推荐答案

在这种情况下,克里斯·巴内斯建议使用,而不是工具栏ActionMenuView(见下面的链接,回复#6)。除此之外,在这个环节,你可以找到一个解决方案,那家伙子类工具栏,以拆分作品的权利。

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

希望它可以帮助你!

这篇关于如何中心工具栏上的操作菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆