问题描述
我希望在我的导航抽屉中的项目旁边有图标,我设置如下:
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.
这篇关于如何将图标添加到导航抽屉中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!