如何在导航抽屉中为项目放置图标

3
我正在开发一个应用程序,其中我使用了导航抽屉,在其中当我尝试为项目放置图标时,它会给我像这样的图像路径: "res/drawale/bg.jpg"。我不知道该怎么做。
以下是我的主要活动代码:
 private void init_navigator() {
    // Navigation Drawer
    mTitle = mDrawerTitle = getTitle();

    mDrawerLayout = (DrawerLayout) findViewById(R.id.main_activity_DrawerLayout);
    mDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.primaryDark));
    mScrimInsetsFrameLayout = (ScrimInsetsFrameLayout) findViewById(R.id.main_activity_navigation_drawer_rootLayout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);
    mDrawerItmes = getResources().getStringArray(R.array.drawer_titles);
  mDrawerItmes = getResources().getStringArray(R.array.nav_drawer_icons);

这是我的string.xml代码:
   <string-array name="drawer_titles">
    <item>About Us</item>
    <item>FeedBack</item>
    <item>Setting</item>
    <item>Share App</item>
    <item>Rate Us</item>
    <item>Logout</item>
</string-array>

<array name="nav_drawer_icons">
    <item>@drawable/bg</item>
    <item>@drawable/bg</item>
    <item>@drawable/bg</item>
    <item>@drawable/bg</item>
    <item>@drawable/b</item>
    <item>@drawable/bg</item>

</array>

1
如果您正在使用NavigationView,那么您可以轻松地创建一个带有标题和图标的菜单,并将菜单作为XML属性填充到NavigationView中。要了解更多信息,请参阅http://www.technotalkative.com/part-4-playing-with-navigationview/。 - George Thomas
但是在这里我怎么给呢?你有什么想法吗? - user6798766
DrawerLayout 意味着一个容器来容纳菜单。你应该在 XML 中明确添加一个导航视图(菜单),并从那里填充菜单。 - George Thomas
2个回答

3
在您的XML导航视图中添加以下内容:

在您的XML导航视图中添加以下内容:

app:menu="@menu/drawer_menu_icons"

然后在菜单文件夹中创建一个名为drawer_menu_icons的新XML文件,并按照以下步骤操作:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group
        android:id="@+id/homegroup"
        android:checkableBehavior="single">
        <item
            android:id="@+id/nav_home"
            android:icon="@drawable/homeicon"
            android:title="Main screen" />
        <item
            android:id="@+id/nav_branches"
            android:icon="@drawable/backspace"
            android:title="Branch selection" />

    </group>
</menu>

并添加你想要的任何图标 :)


0

尝试按照这种方式操作,这正是你所寻找的。

http://www.journaldev.com/9958/android-navigation-drawer-example-tutorial

http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

public class DrawerItemCustomAdapter extends ArrayAdapter<DataModel> {

    Context mContext;
    int layoutResourceId;
    DataModel data[] = null;

    public DrawerItemCustomAdapter(Context mContext, int layoutResourceId, DataModel[] data) {

        super(mContext, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.mContext = mContext;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View listItem = convertView;

        LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
        listItem = inflater.inflate(layoutResourceId, parent, false);

        ImageView imageViewIcon = (ImageView) listItem.findViewById(R.id.imageViewIcon);
        TextView textViewName = (TextView) listItem.findViewById(R.id.textViewName);

        DataModel folder = data[position];


        imageViewIcon.setImageResource(folder.icon);
        textViewName.setText(folder.name);

        return listItem;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接