如何将图标添加到导航抽屉中的项目

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.

这篇关于如何将图标添加到导航抽屉中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Reliable implementation of PBKDF2-HMAC-SHA256 for JAVA(PBKDF2-HMAC-SHA256 for JAVA 的可靠实现)
Correct way to sign and verify signature using bouncycastle(使用 bouncycastle 签名和验证签名的正确方法)
Creating RSA Public Key From String(从字符串创建 RSA 公钥)
Why java.security.NoSuchProviderException No such provider: BC?(为什么 java.security.NoSuchProviderException 没有这样的提供者:BC?)
Generating X509 Certificate using Bouncy Castle Java(使用 Bouncy Castle Java 生成 X509 证书)
How can I get a PublicKey object from EC public key bytes?(如何从 EC 公钥字节中获取 PublicKey 对象?)