添加图标到Android导航抽屉? [英] Add Icons to Android Navigation Drawer?

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

问题描述

我已经搜罗了网上的答案,这个问题,但似乎无法找到一个,甚至在官方Android文档。

我在创建一个应用程序,实现了Android的新的导航抽屉布局的过程。这种布局的文件可以发现这里 和的here

在设计文档以及在一些流行的应用程序,如Facebook和谷歌电流的,抽屉里的物品都在他们面前的图标。

在设计文件本身提到他们,

导航抽屉的内容下
  

导航目标,可以有可选的领先图标以及拖尾计数器。使用计数器来通知用户有关的数据在相应的视图改变的状态。

不幸的是,开发者文档并没有提及如何实现在此设置的图标。它描述了如何实现的项目列表像这样:

 公共类MainActivity延伸活动{
私有String [] mPlanetTitles;
私人的ListView mDrawerList;
...

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);

    mPlanetTitles = getResources()getStringArray(R.array.planets_array)。
    mDrawerList =(ListView控件)findViewById(R.id.left_drawer);

    //设置列表视图适配器
    mDrawerList.setAdapter(新ArrayAdapter<字符串>(这一点,
            R.layout.drawer_list_item,mPlanetTitles));
    //设置列表的点击监听器
    mDrawerList.setOnItemClickListener(新DrawerItemClickListener());

    ...
}
}
 

由于其实现使用getStringArray(),有没有办法,我能想到的修改行项目的XML,包括一个图标。

这是令人费解,因为许多应用程序似乎实现每个文本项目前的图标就好了,和设计文档,甚至提到了增加图标。我觉得必须有一些简单的API调用来完成添加这些图标,但我发现的唯一实现,到目前为止,似乎可笑的复杂。

有谁知道如何将图标添加到导航抽屉的项目?有没有办法用一个简单的API调用做到这一点,或者有这些公司(Facebook,谷歌等),不得不想方设法破解解决该问题?

谢谢大家。

解决方案
  

由于其实现使用getStringArray(),有没有办法,我能想到的修改行项目的XML,包括一个图标。

模型数据为 ListAdapter 有任何没有影响的选择是否由创建该行 ListAdapter 即可有图标。

  

有谁知道如何将图标添加到导航抽屉的项目?

把一个的ImageView 在排布置的 ListAdapter 。填充该的ImageView 通过您的 ListAdapter ,如通过覆盖 getView() ArrayAdapter 。 IOW,你这样做,你做的任何其他的ListView以同样的方式,因为已经做了好几年了。

  

有没有办法用一个简单的API调用做到这一点

这取决于你的简单和API调用。

的定义

I have scoured the web for an answer to this question but cannot seem to find one, even in the official Android documentation.

I am in the process of creating an app that implements Android's new Navigation Drawer layout. Documentation of this layout can be found here and here.

In the design documentation as well as in a number of popular apps such as Facebook and Google Currents, the drawer items have icons in front of them.

The design documentation itself mentions them, under "Content of the Navigation Drawer"

Navigation targets can have optional leading icons as well as trailing counters. Use the counters to inform users about a changed state of data in the corresponding view.

Unfortunately, the Developer documentation makes no mention of how to implement icons in this setup. It describes how to implement the list of items as so:

public class MainActivity extends Activity {
private String[] mPlanetTitles;
private ListView mDrawerList;
...

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mPlanetTitles = getResources().getStringArray(R.array.planets_array);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    // Set the adapter for the list view
    mDrawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_list_item, mPlanetTitles));
    // Set the list's click listener
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    ...
}
}

Because their implementation uses getStringArray(), there is no way I can think of to modify the row item's xml to include an icon.

This is baffling, because many apps seem to implement icons before each text item just fine, and the Design documentation even makes reference to adding icons. I feel that there must be some simple API call to accomplish adding these icons, and yet the only implementation I have found so far seems ridiculously complicated.

Does anyone know how to add icons to the Navigation Drawer items? Is there a way to do this with a simple API call, or have all these companies (Facebook, Google, etc) had to find ways to hack around the problem?

Thanks everyone.

解决方案

Because their implementation uses getStringArray(), there is no way I can think of to modify the row item's xml to include an icon.

The choice of model data for a ListAdapter has no impact whatsoever on whether the rows created by that ListAdapter can have icons.

Does anyone know how to add icons to the Navigation Drawer items?

Put an ImageView in your row layout for the ListAdapter. Populate that ImageView via your ListAdapter, such as by overriding getView() on your ArrayAdapter. IOW, you do it the same way as you do for any other ListView, as has been done for years.

Is there a way to do this with a simple API call

That depends on your definitions of "simple" and "API call".

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

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