2014-01-13 89 views
0

我實現了導航抽屜,但現在我想爲每行文本插入圖像。 我在官方指南中找不到信息。謝謝Android NavigationDrawer圖像

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

回答

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, ...}; 

最後你改變這個李NE在你的代碼

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如果你想有一個很好的理解。