1
我一直在試圖弄清楚這一點在相當一段時間。我瀏覽過這個網站和其他許多網站,找不到任何有用的東西。我只需要在我的導航抽屜中的每個項目旁邊放置圖標。我目前正在使用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商店之前,這基本上是我需要做的最後一件事。提前致謝!
你需要使用自定義列表視圖下面這段代碼。 AppCompat示例http://stackoverflow.com/questions/20088247/navigation-drawer-with-backword-compatibility-android/20088711#20088711 – Raghunandan