如何图标添加到项目中抽屉式导航 [英] How to add icons to items in a navigation drawer

查看:119
本文介绍了如何图标添加到项目中抽屉式导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有图标的旁边,我在我的抽屉式导航栏项目,我已经建立了这样的:

 标题= getResources()getStringArray(R.array.array1)。
    。图标= 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(新ArrayAdapter<字符串>(这一点,R.layout.drawer_list_item,标题));
 

我知道这个过程必须涉及添加图像视图文本视图是在XML的drawer_list_item,但我不知道该怎么做。什么是实现这一目标的最佳方式是什么?

这是我drawer_list_item.xml:

 < TextView中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@机器人:ID / text1中
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:textAppearance =机器人:ATTR / textAppearanceListItemSmall
机器人:重力=center_vertical
机器人:以下属性来=16DP
机器人:paddingRight =16DP
机器人:文字颜色=#111
机器人:背景=机器人:ATTR / activatedBackgroundIndicator
机器人:=了minHeight:/>中的Andr​​oid ATTR /列表preferredItemHeightSmall?
 

解决方案

导航抽屉本质上是一个列表视图。创建与任何设计你想有一个drawer_item.xml(文字+ ImageView的),并把它传递给一个Ar​​rayAdapter。然后填充列表视图(在getview方法)时,分配的ImageView到您所选择的绘制。

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));

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?

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"/>

解决方案

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆