动作条与自定义菜单项自定义布局 [英] ActionBar with custom layout with custom menu item

查看:134
本文介绍了动作条与自定义菜单项自定义布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过放的ImageView 在中间进行自定义操作栏,并有另一个图标与右侧动作条像这样一些信息:

I want to make custom action bar by put ImageView in the middle and have another icon with some info on the right side of actionbar like this:

我可以的ImageView在中间了,但是问题是

I can make imageview in the middle already but problem is

  • 当我吹布局 onCreateOptionMenu(),并设置 showAsAction总是= 在右侧视图下动作条的所有空间像这样
  • when i inflate layout in onCreateOptionMenu() and set showAsAction="always" the view on the right take all space of action bar like this

但如果我将其设置为从不中间的图像显示,但菜单项将消失

but if i set it to "never" the middle image will show but menu item will gone

这是我的code:

private void setActionBar(){
        actionBar = getActionBar();
        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayUseLogoEnabled(true);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);

        LayoutInflater linflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        View view = linflater.inflate(R.layout.actionbar_layout, null);
        ActionBar.LayoutParams lp = new ActionBar.LayoutParams(
                ActionBar.LayoutParams.WRAP_CONTENT,
                ActionBar.LayoutParams.WRAP_CONTENT);
        lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
        view.setLayoutParams(lp);
        actionBar.setCustomView(view);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.actionbar_right_icon, menu);
        MenuItem menuItem = menu.findItem(R.id.user_info);

        View rightIcon = menuItem.getActionView();
        RelativeLayout rightIconLayout = (RelativeLayout) rightIcon.findViewById(R.id.actionbar_right_icon);
        TextView user = (TextView) rightIconLayout.findViewById(R.id.actionbar_user);
        TextView id = (TextView) rightIconLayout.findViewById(R.id.actionbar_id);

        user.setText("User:test");
        id.setText("ID:xxxxx");

        return true;
    }

菜单/ actionbar_right_icon

menu/actionbar_right_icon

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    >
    <item 
        android:id="@+id/user_info"
        android:actionLayout="@layout/actionbar_right_icon_layout"
        android:icon="@drawable/user_icon"
        android:showAsAction="always"

        />
</menu>

我该如何解决这个问题?

how can i fix this?

感谢你在前进

推荐答案

动作条有customView支持。不要使用 onCreateOptionsMenu() R.menu.actionbar_right_icon 。相反,创建一个布局,而它所有的你有平局的看法。

ActionBar has customView support. Do not use onCreateOptionsMenu() and R.menu.actionbar_right_icon. Instead create a layout that takes all of the views you have draw.

您可以像下面

LayoutParams layout_params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
getSupportActionBar().setCustomView(custom_view, layout_params); 

这篇关于动作条与自定义菜单项自定义布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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