简体   繁体   English

Android-在操作栏中添加图标

[英]Android - add icon in actionbar

I need to add an icon at the top right of the menu in the ActionBar when an item is selected from the NavigationDrawer menu. NavigationDrawer菜单中选择一个项目后,我需要在ActionBar菜单的右上角添加一个图标。

So I have create a new file called for example ' blog ' that extends Fragment and I have write in it the onCreateOptionsMenu method that should manage the menu action bar right? 因此,我创建了一个名为“ blog ”的新文件,该文件扩展了Fragment并在其中写入了应管理菜单操作栏的onCreateOptionsMenu方法。

This is my code: 这是我的代码:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;

public class Blog extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.blog, container, false);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.blog_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }
}

Android studio write in red this line of code: Android studio用红色写这行代码:

MenuInflater inflater = getMenuInflater();

so I think that it is wrong but I do not understand what is the problem...any help? 所以我认为这是错误的,但是我不明白这是什么问题……有什么帮助吗?

To control your menu inside a Fragment , you need to call this method: 要在Fragment控制菜单,您需要调用以下方法:

setHasOptionsMenu(true);  

inside onCreateView method. 里面的onCreateView方法。 Then, you need the MenuInflater as follows: 然后,您需要使用MenuInflater ,如下所示:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    MenuItem itemBlog = menu.add(Menu.NONE, // Group ID
                                 R.id.blog_item, // Item ID
                                 1, // Order
                                 R.string.blog_item); // Title
    itemBlog.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); // ShowAsAction
    itemBlog.setIcon(R.drawable.ic_action_blog); // Icon
    // add your item before calling the super method
    super.onCreateOptionsMenu(menu,inflater);
}    

I don't know if this is the case, however if you use the AppCompat library, you should do as follows: 我不知道是否是这种情况,但是,如果您使用AppCompat库,则应执行以下操作:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    MenuItem itemBlog = menu.add(Menu.NONE, // Group ID
                                 R.id.action_blog, // Item ID
                                 101, // Order
                                 "Blog"); // Title
    // To showAsAction attribute, use MenuItemCompat (set to always)
    MenuItemCompat.setShowAsAction(itemBlog, MenuItem.SHOW_AS_ACTION_ALWAYS);
    itemBlog.setIcon(R.drawable.ic_action_blog);
    super.onCreateOptionsMenu(menu, inflater);
}

This works well. 这很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM