简体   繁体   English

如何在商品上添加图标(android)?

[英]How can i add icon on items (android)?

I'm hoping that you can help me guys, i don't have any idea on adding icon on the menu. 我希望您能对我有所帮助,我对在菜单上添加图标一无所知。 Advance thanks. 提前谢谢。

I need to look my menu the same with this one: http://developer.android.com/design/media/navigation_drawer_titles_icons.png 我需要使菜单与此菜单相同: 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>

Than you need to create ListView with custom adapter. 比您需要使用自定义适配器创建ListView。 Here you can find example how to do it. 在这里,您可以找到示例操作方法。

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

Hope it will help you. 希望对您有帮助。

You can try something like this 您可以尝试这样的事情

Here you need to add your own Adapter that accepts list items that contain string and icon. 在这里,您需要添加自己的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());

        ...
    }
}

your list item can look something like this 您的清单项目可能看起来像这样

<?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>

and your adapter will look something like this, call it list_item_layout.xml 然后您的适配器将看起来像这样,将其称为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;
    }

}

The IndexItem I use in the adapter looks like this 我在适配器中使用的IndexItem看起来像这样

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

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

After you created all of this replace the Adapter in the setAdapter method with your adapter like so 创建完所有这些之后,将setAdapter方法中的Adapter替换为您的适配器,如下所示

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

the getIndexItems method can look something like this 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;
}

try this : 尝试这个 :

add this in your main_activity.xml : 将此添加到您的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>

add this in your Main_Activity.java : 将此添加到您的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;
}
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM