問題描述
我希望在我的導航抽屜中的項目旁邊有圖標,我設置如下:
I would like to have icons next to my items in my navigation drawer that I have set up like this:
Titles = getResources().getStringArray(R.array.array1);
Icons = getResources().getIntArray(R.array.icons);
mDrawerLayout = (DrawerLayout)findViewById(R.id.Welcome);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerList.setAdapter( new ArrayAdapter<String>(this, R.layout.drawer_list_item, Titles));
我知道該過程必須涉及將圖像視圖添加到位于抽屜列表項的 XML 中的文本視圖中,但我不知道該怎么做.實現這一目標的最佳方法是什么?
I know that the process must involve adding an image view to the text view that is in the XML for the drawer_list_item but I'm not sure how to do that. What is the best way to accomplish this?
這是我的drawer_list_item.xml:
This is my 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="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#111"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"/>
推薦答案
導航抽屜本質上是一個列表視圖.使用您想要的任何布局(文本+圖像視圖)創建一個drawer_item.xml,并將其傳遞給arrayAdapter.然后在填充 listView 時(在 getview 方法中),將 imageView 分配給您選擇的可繪制對象.
The Navigation Drawer is essentially a list view. Create a drawer_item.xml with whatever layout you want (text+imageview) and pass it to the arrayAdapter. Then when populating the listView (in the getview method), assign the imageView to the drawable of your choosing.
這篇關于如何將圖標添加到導航抽屜中的項目的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!