Android导航抽屉图像

时间:2014-01-13 23:33:51

标签: android

我实现了导航抽屉,但现在我想为每行文字插入一个图像。 我没有在官方指南中找到信息。感谢

mTitle = mDrawerTitle = getTitle();
        mGalaxyTitles = getResources().getStringArray(R.array.planets_array);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        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, mGalaxyTitles));
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
        if (savedInstanceState == null) {
            selectItem(0);}
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,         
                R.drawable.ic_drawer,  
                R.string.hello_world,  
                R.string.hello_world  
                ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); 
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); 
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

1 个答案:

答案 0 :(得分:0)

您必须像创建任何类型的列表一样创建自己的适配器。

例如,你让你的R.layout.drawer_list_ite看起来像这样:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="6dip" android:orientation="horizontal">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

        android:ellipsize="marquee"
        android:singleLine="true"  " />

</LinearLayout> 

然后,您创建自己的适配器

public class MyArrayAdapter extends ArrayAdapter<String> {
  private final Context context;
  private final String[] values;
  private final int layout;
 private final int []images;

  public MyArrayAdapter(Context context, int layout, String[] values, int [] images) {
    super(context,  layout, values);
    this.context = context;
    this.values = values;
    this.layout=layout;
    this.images=images;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(layout, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.text);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
    textView.setText(values[position]);
      imageView.setImageResource(images[position]);


    return rowView;
  }
} 

然后,在您的活动中,您将创建一系列图像资源,例如:

int[] mImages = new int[]{R.drawable.IMAGE1,R.drawable.IMAGE2,R.drawable.IMAGE3, ...};

最后你在代码中更改了这一行

mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mGalaxyTitles));

到此处,您将addapter分配到列表:

mDrawerList.setAdapter(new MyArrayAdapter<String>(this, R.layout.drawer_list_item, mGalaxyTitles, mImages));

如果您想要很好的理解,可以关注vogella's tutorial