How to add icons to items in a navigation drawer(如何将图标添加到导航抽屉中的项目)
问题描述
我希望在我的导航抽屉中的项目旁边有图标,我设置如下:
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.
这篇关于如何将图标添加到导航抽屉中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将图标添加到导航抽屉中的项目
基础教程推荐
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 存储 20 位数字的数据类型 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
