【问题标题】:Adding Icons next to items in Navigation Drawer在导航抽屉中的项目旁边添加图标
【发布时间】:2014-01-12 08:06:27
【问题描述】:

我一直试图弄清楚这个问题已经有一段时间了。我已经浏览了这个网站和许多其他网站,但找不到任何有用的东西。我只想要导航抽屉中每个项目旁边的图标。我目前使用的是谷歌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>
     ...

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

【问题讨论】:

标签: android navigation icons navigation-drawer drawer


【解决方案1】:

只需在标题下方添加此代码

icon = new int[] { R.drawable.homeitem,
            R.drawable.ic_cheryyitem};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多