我想在菜单

时间:2017-05-22 17:47:33

标签: java android

我想在菜单中的项目中添加图片。我在互联网上找到了很多解决方案,但似乎都没有。

    public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(0,0,0,"pic 1");
    menu.getItem(0).setIcon(R.mipmap.pic1);

(没有任何反应)

此外,我正在尝试使用上下文操作模式创建菜单,如下所示:

>   private ActionMode.Callback callback = new ActionMode.Callback() {
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            getMenuInflater().inflate(R.menu.action_mode_context_menu_layout,menu);
            return false;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId())
            {
                case R.id.left:
                    TV2.setText("Selected: Left");
                    TV2.setGravity(Gravity.LEFT);
                    break;
                case R.id.right:
                    TV2.setText("Selected: Right");
                    TV2.setGravity(Gravity.RIGHT);
                    break;
                case R.id.middle:
                    TV2.setText("Selected: Middle");
                    TV2.setGravity(Gravity.CENTER);
                    break;
            }
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_2);
        TV2 = (TextView)findViewById(R.id.textView2);
        TV2.setOnLongClickListener(new View.OnLongClickListener()
        {
            @Override
            public boolean onLongClick(View v) {
                ActionMode mode = ((Activity)v.getContext()).startActionMode(callback);
                return true;
            }
        });
}

我也尝试过这样:

ActionMode mode = ((AppCompatActivity)v.getContext()).startSupportActionMode(callback);

并且两种情况都没有发生

1 个答案:

答案 0 :(得分:0)

您是否只想将图标添加到导航视图?

您只需使用图标标记

即可
    <item
        android:id="@+id/nav_clubs"
        android:icon="@drawable/ic_menu_clubs"
        android:title="cool"/>

并将其添加到抽屉中:

  app:menu="@menu/your_group_of_item"