如何改变的动作条上的菜单项的位置 [英] How to change the position of menu items on actionbar

查看:125
本文介绍了如何改变的动作条上的菜单项的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序中,我必须添加自定义布局上动作条。添加自定义布局完成,但是当我在动作条我的自定义布局添加菜单项,改变它的位置从动作条右侧的动作条的中心。下面是我迄今所取得的图像。

I'm developing one application in which I have to add a custom layout on actionbar. Adding the custom layout is done, but when I'm adding the menu items on actionbar my custom layout changes it's position from right of the actionbar to center of the actionbar. Below is the image what I have achieved so far.

我想是这样的动作条。

自定义布局(黄色按钮),在动作条和菜单项的中间右侧大部分。

Custom layout(yellow button) at the right most part of actionbar and menu items in the middle.

添加我的code。使用原生的Andr​​oid动作条来实现这个自定义布局:

Adding my code to achieve this custom layout using native android actionbar:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    actionBar = getActionBar();
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayUseLogoEnabled(false);
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    cView = getLayoutInflater().inflate(R.layout.header, null);
    actionBar.setCustomView(cView);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.websearch_menu, menu);
    return true;

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

    case android.R.id.home:
        finish();

    default:
        return super.onOptionsItemSelected(item);
    }
}

如何才能实现这一目标?

How can this be achieved?

任何形式的帮助将是AP preciated。

Any kind of help will be appreciated.

推荐答案

我假设你正在设置自定义视图操作栏中的自定义视图。如果您使用的是ActionbarSherlock而是可以添加自定义视图作为一个正常的菜单项,并使用 .setActionView(yourCustomView或yourCustomLayoutId)设置为它的操作视图。然后,您可以设置 .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)显示视图。只要确保你的自定义视图设置宽度 WRAP_CONTENT 或否则将推动其他菜单项关闭屏幕。这也可能工作没有ActionbarSherlock,但我还没有尝试过那里。

I assume you are setting your custom view as a custom view in the action bar. If you are using ActionbarSherlock you can instead add the custom view as a normal menu item and set an action view for it using .setActionView(yourCustomView or yourCustomLayoutId). You can then set .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS) to display the view. Just make sure your custom view is set to width wrap_content or otherwise it will push the other menu items off the screen. This might also work without ActionbarSherlock, but I haven't tried it there yet.

编辑:尝试修改你的 onCreateOptionsMenu 方法

Try modifying your onCreateOptionsMenu method

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.websearch_menu, menu);

    // add this
    menu.add(Menu.NONE, 0, Menu.NONE, "custom")
        .setActionView(R.layout.header)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

    return true;
}

和从onCreate方法删除该

and remove this from your onCreate method

actionBar.setDisplayShowCustomEnabled(true);
cView = getLayoutInflater().inflate(R.layout.header, null);
actionBar.setCustomView(cView);

编辑:

如果您需要参考的意见,从你的自定义布局,您可以在 onCreateOptionsMenu 办法做到这一点如下(注:我在写这篇code浏览器,因此不能保证我没有做一个错字或东西):

If you need to reference the views from your custom layout, you can do this in your onCreateOptionsMenu method as follows (note: I wrote this code in the browser, so no guarantee that I didn't make a typo or something):

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // inflate the menu and add the item with id viewId,
    // which has your layout as actionview

    View actionView = menu.getItem(viewId).getActionView();
    View viewFromMyLayout = actionView.findViewById(R.id.viewFromMyLayout);

    // here you can set listeners to your view or store a reference 
    // to it somewhere, so you can use it later

    return true;
}

这篇关于如何改变的动作条上的菜单项的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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