如何在项目上添加图标(android)?

时间:2013-11-12 09:40:52

标签: android

我希望你能帮助我们,我不知道在菜单上添加图标。谢谢。

我需要看一下我的菜单:http://developer.android.com/design/media/navigation_drawer_titles_icons.png

<resources>
<string name="app_name">Navigation Drawer Example</string>
<string-array name="planets_array">
    <item>Mercury</item>
    <item>Venus</item>
    <item>Earth</item>
    <item>Mars</item>
    <item>Jupiter</item>
    <item>Saturn</item>
    <item>Uranus</item>
    <item>Neptune</item>
</string-array>
<string name="drawer_open">Open navigation drawer</string>
<string name="drawer_close">Close navigation drawer</string>
<string name="action_websearch">Web search</string>
<string name="app_not_available">Sorry, there\'s no web browser available</string>
</resources>

3 个答案:

答案 0 :(得分:0)

您需要使用自定义适配器创建ListView。在这里你可以找到如何做的例子。

http://theopentutorials.com/tutorials/android/listview/android-custom-listview-with-image-and-text-using-arrayadapter/

希望它会对你有所帮助。

答案 1 :(得分:0)

您可以尝试这样的事情

在这里,您需要添加自己的Adapter,以接受包含字符串和图标的列表项。

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

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

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

        // Here where the adapter is set, add your own adapter
        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_list_item, mPlanetTitles));
        // Set the list's click listener
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        ...
    }
}

您的列表项可能看起来像这样

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/button_background_white"
    android:orientation="horizontal" >

    <ImageView 
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:contentDescription="@null"
        android:layout_margin="5dp"
        android:padding="5dp"/>

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_margin="5dp"
        android:padding="5dp"
        android:textColor="@color/dark_gray"
        android:textSize="18sp" />

</LinearLayout>

并且您的适配器看起来像这样,称之为list_item_layout.xml

public class IndexAdapter extends BaseAdapter {

    private List<IndexItem> items;
    private Context context;

    public IndexAdapter(Context context, List<IndexItem> items) {
        this.items = items;
        this.context = context;
    }

    @Override
    public int getCount() {
        return (items != null && items.size() > 0) ? items.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return (items != null && items.size() > 0) ? items.get(i) : null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int pos, View convertView, ViewGroup arg2) {

        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.index_list_item_layout, null);
        }

        ((ImageView) convertView.findViewById(R.id.image)).setImageResource(items.get(pos).iconID);
        ((TextView) convertView.findViewById(R.id.name)).setText(items.get(pos).name);

        return convertView;
    }

}

我在适配器中使用的IndexItem看起来像这样

public class IndexItem {
    public final String name;
    public final int iconID;

    public IndexItem(String name, int iconID) {
        this.name = name;
        this.iconID = iconID;
    }
}

创建完所有内容之后,使用您的适配器替换Adapter方法中的setAdapter

List<IndexItem> yourIndexList = getIndexItems(); //getIndexItems will populate your text and icons

// Here where the adapter is set, add your own adapter
mDrawerList.setAdapter(new IndexAdapter(this, yourIndexList));

getIndexItems方法看起来像这样

public List<IndexItem> getIndexItems() {
    List<IndexItem> returnList = new ArrayList<IndexItem>();

    Stirng [] text = {"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune"};
    int [] icons = {R.drawable.mercury,R.drawable.venus,R.drawable.earth,R.drawable.mars,R.drawable.jupiter,R.drawable.saturn,R.drawable.uranus,R.drawable.neptune};

    for (int i = 0; i < text.length; i++) {
        returnList.add(new IndexItem(text[i], icons[i]));
    }

    return returnList;
}

答案 2 :(得分:0)

试试这个:

在main_activity.xml中添加:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="" />

<ListView
    android:id="@+id/drawer"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#F3F3F4"
    android:choiceMode="singleChoice"
    android:dividerHeight="0dp"
    />
</android.support.v4.widget.DrawerLayout>

在Main_Activity.java中添加:

public class MainActivity extends Activity{

private ListView listView;
private DrawerLayout drawerLayout;
private MyActionBarDrawerToggle drawerToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);

listView = (ListView) findViewById(R.id.drawer);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

MenuAdapter adapter = new MenuAdapter(this);
adapter.add(new MenuModel("Title Menu", 0, 0, true));
adapter.add(new MenuModel("Food",R.drawable.your_drawable, 43, false));
adapter.add(new MenuModel("Beverage", R.drawable.your_drawable, 17, false));
adapter.add(new MenuModel("Juice",R.drawable.your_drawable, 10, false));
adapter.add(new MenuModel("Snack",R.drawable.your_drawable, 7, false));
adapter.add(new MenuModel("Other", 0, 0, true));
adapter.add(new MenuModel("Setting",R.drawable.action_settings, 0, false));

listView.setAdapter(adapter);

listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        if(arg2 == 1){
            Intent i = new Intent(MainActivity.this, Food.class);
            startActivity(i);
        } else if(arg2 == 2){
            Intent i = new Intent(MainActivity.this, Beverage.class);
            startActivity(i);
        } else if(arg2 == 3){
            Intent i = new Intent(MainActivity.this, Juice.class);
            startActivity(i);
        } else if(arg2 == 4){
            Intent i = new Intent(MainActivity.this, Snack.class);
            startActivity(i);
        } else if(arg2 == 6){
            Intent i = new Intent(MainActivity.this, Gallery_Menu_Paket_Active.class);
            startActivity(i);
        }

    }
});

drawerToggle = new MyActionBarDrawerToggle(this, drawerLayout);
drawerLayout.setDrawerListener(drawerToggle);

getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);

}

private class MyActionBarDrawerToggle extends ActionBarDrawerToggle{

    public MyActionBarDrawerToggle(Activity mActivity,DrawerLayout mDrawerLayout){
            super(mActivity,mDrawerLayout,R.drawable.ic_drawer,R.string.app_name,R.string.app_name);
    }

    @Override
    public void onDrawerClosed(View view) {
            invalidateOptionsMenu();
    }

    @Override
    public void onDrawerOpened(View drawerView) {
            invalidateOptionsMenu();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        drawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(drawerToggle.onOptionsItemSelected(item)) {
            return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean drawerOpen = drawerLayout.isDrawerOpen(listView);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}

public class MenuAdapter extends ArrayAdapter<MenuModel> {

    public MenuAdapter(Context context) {
        super(context, 0);
    }

    @Override
    public int getViewTypeCount() {
            return 2;
    }

    @Override
    public int getItemViewType(int position) {
            return getItem(position).isGroupTitle() ? 0 : 1;
    }

    @Override
    public boolean isEnabled(int position) {
            return !getItem(position).isGroupTitle();
    }

     @Override
     public View getView(int position, View convertView, ViewGroup parent) {

             MenuViewHolder holder = null;

             int type = getItemViewType(position);
             MenuModel menu= getItem(position);

             if (convertView == null) {

                     switch (type) {
                     case 0:

                             convertView = LayoutInflater.from(getContext()).inflate(R.layout.menu_row_group, null);
                             holder = new MenuViewHolder((TextView) convertView.findViewById(R.id.group_title),null, null);
                             break;
                     case 1:
                             convertView = LayoutInflater.from(getContext()).inflate(R.layout.menu_row, null);

                             holder = new MenuViewHolder(
                                             (TextView) convertView.findViewById(R.id.menu_title),
                                             (ImageView) convertView.findViewById(R.id.row_icon),
                                             (TextView) convertView.findViewById(R.id.row_counter));
                             break;
                     }
                     convertView.setTag(holder);
             } else {
                     holder = (MenuViewHolder) convertView.getTag();
             }


             holder.titleView.setText(menu.getTitle());
             if(type != 0){
                     holder.iconView.setImageResource(menu.getIcon());
                     if (menu.getCounter() > 0) {
                             holder.counterView.setVisibility(View.VISIBLE);
                             holder.counterView.setText(menu.getCounter() + "");
                     } else {
                             holder.counterView.setVisibility(View.GONE);
                     }
             }
             return convertView;
     }

     public class MenuViewHolder {
         public final TextView titleView;
         public final ImageView iconView;
         public final TextView counterView;

         public MenuViewHolder(TextView titleView, ImageView iconView,
                         TextView counterView) {
                 this.titleView = titleView;
                 this.iconView = iconView;
                 this.counterView = counterView;
         }
 }

}

public class MenuModel {

    private String title;
    private int icon;
    private int counter;
    private boolean isGroupTitle;

    public MenuModel(String title, int icon, int counter, boolean isGroupTitle) {
        this.title = title;
        this.icon = icon;
        this.counter = counter;
        this.isGroupTitle = isGroupTitle;
    }

    public String getTitle() {
        return title;
}

public void setTitle(String title) {
        this.title = title;
}

public int getIcon() {
        return icon;
}

public void setIcon(int icon) {
        this.icon = icon;
}

public int getCounter() {
        return counter;
}

public void setCounter(int counter) {
        this.counter = counter;
}

public boolean isGroupTitle() {
        return isGroupTitle;
}

public void setGroupTitle(boolean isGroupTitle) {
        this.isGroupTitle = isGroupTitle;
}
}

}