如何在项目上添加图标(android)?
我希望你们能帮我,伙计们,我不知道如何在菜单上添加图标。提前谢谢 我需要看我的菜单和这个一样:如何在项目上添加图标(android)?,android,Android,我希望你们能帮我,伙计们,我不知道如何在菜单上添加图标。提前谢谢 我需要看我的菜单和这个一样: 导航抽屉示例 水星 维纳斯 土 火星 木星 土星 天王星 海王星 打开导航抽屉 关闭导航抽屉 网络搜索 抱歉,没有可用的web浏览器 您需要使用自定义适配器创建ListView。在这里你可以找到如何做的例子 希望它能帮助你。你可以试试这样的东西 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="
导航抽屉示例
水星
维纳斯
土
火星
木星
土星
天王星
海王星
打开导航抽屉
关闭导航抽屉
网络搜索
抱歉,没有可用的web浏览器
您需要使用自定义适配器创建ListView。在这里你可以找到如何做的例子
希望它能帮助你。你可以试试这样的东西
<?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>
public class IndexItem {
public final String name;
public final int iconID;
public IndexItem(String name, int iconID) {
this.name = name;
this.iconID = iconID;
}
}
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;
}
在这里,您需要添加自己的适配器
,它接受包含字符串和图标的列表项
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());
...
}
}
我在适配器中使用的IndexItem如下所示
<?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>
public class IndexItem {
public final String name;
public final int iconID;
public IndexItem(String name, int iconID) {
this.name = name;
this.iconID = iconID;
}
}
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;
}
创建完所有这些之后,用适配器替换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));
试试这个:
将此添加到您的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;
}
}
}
公共类MainActivity扩展活动{
私有列表视图列表视图;
私人抽屉布局;
私用MyActionBarDrawerToggle抽屉;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main_活动);
listView=(listView)findViewById(R.id.drawer);
抽屉布局=(抽屉布局)findViewById(R.id.抽屉布局);
drawerLayout.setDrawerShadow(R.drawable.drawer\u shadow,GravityCompat.START);
MenuAdapter=新的MenuAdapter(此);
add(新菜单模型(“标题菜单”,0,0,true));
add(新菜单模型(“Food”,R.drawable.your_drawable,43,false));
添加(新菜单模型(“饮料”,R.drawable.your_drawable,17,false));
add(新菜单模型(“Juice”,R.drawable.your_drawable,10,false));
add(新菜单模型(“零食”,R.drawable.your_drawable,7,false));
add(新菜单模型(“其他”,0,0,true));
add(新菜单模型(“设置”,R.drawable.action_设置,0,false));
setAdapter(适配器);
setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共视图单击(AdapterView arg0、视图arg1、整型arg2、长型arg3){
//TODO自动生成的方法存根
如果(arg2==1){
意向i=新意向(MainActivity.this,Food.class);
星触觉(i);
}else if(arg2==2){
意向i=新意向(MainActivity.this、Beverage.class);
星触觉(i);
}else if(arg2==3){
意向i=新意向(MainActivity.this,Juice.class);
星触觉(i);
}else if(arg2==4){
意向i=新意向(MainActivity.this,Snack.class);
星触觉(i);
}else if(arg2==6){
意向i=新意向(MainActivity.this,Gallery\u Menu\u Paket\u Active.class);
星触觉(i);
}
}
});
抽屉切换=新的MyActionBarDrawerToggle(此为抽屉布局);
抽屉布局。设置抽屉链接器(抽屉切换);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
}
私有类MyActionBarDrawerToggle扩展了ActionBarDrawerToggle{
公共MyActionBarDrawerToggle(活动权限、抽屉布局MDDrawerLayout){
super(mActivity,mDrawerLayout,R.drawable.ic_drawer,R.string.app_name,R.string.app_name);
}
@凌驾
公共无效onDrawerClosed(视图){
无效操作菜单();
}
@凌驾
打开图纸上的公共空白(视图抽屉视图){
无效操作菜单();
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
@凌驾
后期创建时受保护的空(捆绑包savedInstanceState){
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
@凌驾
公共无效OnConfiguration已更改(配置newConfig){
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
如果(抽屉切换选项项选定(项目)){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
@凌驾
公共布尔值OnPrepareOptions菜单(菜单){
布尔值drawerropen=drawerLayout.isDrawerOpen(listView);
menu.findItem(R.id.action\u设置).setVisible(!drawerropen);
返回super.onPrepareOptions菜单(菜单);
}
公共类MenuAdapter扩展了ArrayAdapter{
公共菜单适配器(上下文){
超级(上下文,0);
}
@凌驾
public int getViewTypeCount(){
返回2;
}
@凌驾
public int getItemViewType(int位置){
返回getItem(position).isGroupTitle()?0:1;
}
@凌驾
公共布尔值isEnabled(整型位置){
return!getItem(position).isGroupTitle();
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
MenuViewHolder holder=null;
int type=getItemViewType(位置);
MenuModel menu=getItem(位置);
if(convertView==null){
开关(类型){
案例0:
convertView=LayoutInflater.from(getContext()).flate(R.layout.menu\u row\u group,null);
holder=newmenuviewholder((TextView)convertView.findViewById(R.id.group_title),null,null);
打破
案例1:
convertView=LayoutInflater.from(getContext()).flate(R.layout.menu_行,null);
保持架=新的菜单项(
(TextView)convertView.findViewById(R.id.menu_title),
(ImageView)convertView.findViewById(R.id.row_图标),
(特克斯