在导航抽屉中的项目旁边添加图标

时间:2014-01-12 08:06:27

标签: android navigation icons navigation-drawer drawer

我一直试图解决这个问题很长一段时间。我看过这个网站和其他许多网站,但找不到任何可行的网站。我只想在导航抽屉中的每个项目旁边放置图标。我目前正在使用Google navigation drawer sample app使用的方法。

MainActivity.java中的

我有以下内容:

    mColorTitles = getResources().getStringArray(R.array.colors_array);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);
    mColorIcons = getResources().getStringArray(R.array.color_icons);
    adapter = new ArrayAdapter<String>(this, R.layout.drawer_list_item, mColorTitles);

 // set a custom shadow that overlays the main content when the drawer opens
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
 // set up the drawer's list view with items and click listener      
    mDrawerList.setAdapter(adapter);
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

我的drawer_list_item.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:paddingRight="16dp"
android:textColor="#000"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"/>

它目前只是让导航抽屉显示数组中的颜色标题。我在另一个数组中有我想要的图标,它们遵循与我希望它们与颜色相关联的完全相同的顺序。我根本不知道如何开始将该数组中的图标插入导航项

如果有帮助,这就是我的数组在strings.xml中的样子(不是完整的代码)

<string-array name="colors_array">        
    <item>Home</item>
    <item>Cherry</item>
    <item>Crimson</item>
     ...
<array name="color_icons">
    <item>@drawable/homeicon</item>
    <item>@drawable/cherryicon</item>
    <item>@drawable/crimsonicon</item>
     ...

我已经尝试在drawer_list_item中放置一个drawable,它可以工作,但(当然)它总是把相同的那个放在那里。我想不出根据颜色改变它的方法。我对android编程比较陌生,所以如果我遗漏了一些简单的东西,我很抱歉。如果你可以帮助我,我会非常感激,因为这基本上是我在将应用程序发布到Play商店之前需要做的最后一件事。提前谢谢!

1 个答案:

答案 0 :(得分:1)

只需在标题

下添加此代码即可
icon = new int[] { R.drawable.homeitem,
            R.drawable.ic_cheryyitem};