我想在我的抽屉菜单项中添加图标,但是我无法想到这样做的方法。这就是抽屉的样子: 如何在文本左侧添加图标,如下所示:
代码:
currentFragment = dashboard;
SetSupportActionBar(toolbar);
leftDataSet = new List<string>();
leftDataSet.Add(GetString(Resource.String.dashboard_title));
leftDataSet.Add(GetString(Resource.String.smoke_sensor_title));
leftDataSet.Add(GetString(Resource.String.motion_sensor_title));
leftDataSet.Add(GetString(Resource.String.door_sensor_title));
leftDataSet.Add(GetString(Resource.String.temperature_sensor_title));
leftDataSet.Add(GetString(Resource.String.gallery_title));
leftAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, leftDataSet);
leftDrawer.Adapter = leftAdapter;
//
// Change to the appropriate fragment view when the user clicks on an item from the left drawer
//
leftDrawer.ItemClick += (sender, args) => {
SelectItem(args.Position);
};
drawerToggle = new MenuDrawer(
this, drawerLayout, Resource.String.open_drawer, Resource.String.dashboard_title //Closed Message (not used but required to declare)
);
drawerLayout.SetDrawerListener(drawerToggle);
SupportActionBar.SetHomeButtonEnabled(true);
SupportActionBar.SetDisplayShowTitleEnabled(true);
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
drawerToggle.SyncState();
}
private void SelectItem(int position) {
switch (position) {
case 0:
ShowFragment(dashboard);
SupportActionBar.SetTitle(Resource.String.dashboard_title);
break;
case 1:
ShowFragment(smokeSensor);
SupportActionBar.SetTitle(Resource.String.smoke_sensor_title);
break;
case 2:
ShowFragment(motionSensor);
SupportActionBar.SetTitle(Resource.String.motion_sensor_title);
break;
case 3:
ShowFragment(doorSensor);
SupportActionBar.SetTitle(Resource.String.door_sensor_title);
break;
case 4:
ShowFragment(temperatureSensor);
SupportActionBar.SetTitle(Resource.String.temperature_sensor_title);
break;
case 5:
ShowFragment(gallery);
SupportActionBar.SetTitle(Resource.String.gallery_title);
break;
default:
ShowFragment(currentFragment);
break;
}
}
非常感谢任何帮助!
编辑:我创建了一个已覆盖的视图,但它确实有效,但我还是不确定如何添加图标:
using System;
using System.Collections.Generic;
using Android.Widget;
using Android.Content;
using Android.Views;
namespace Homecheck.Adapters {
public class CustomListView : BaseAdapter<string> {
//
// This adapter overrides the basic Android ListView design with a custom one.
// The custom layout can display pictures along with the text, and the design can be changed very easilly using xml.
//
private List<string> items;
private Context context;
public CustomListView(Context context, List<string> items) {
this.context = context;
this.items = items;
}
//
// Counts how many items are there in the ListView (Dashboard, Smoke Sensor, Motion sensor etc.)
// They reside in values*/Strings.xml (* depends on the language)
//
public override int Count {
get { return items.Count; }
}
public override long GetItemId(int position) {
return position;
}
public override string this[int position] {
get { return this.items[position]; }
}
//
// Populates each row in the ViewList with an image and a text.
//
public override View GetView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
row = LayoutInflater.From(this.context).Inflate(Resource.Layout.MenuDrawer, null, false);
}
TextView text = row.FindViewById<TextView>(Resource.Id.text);
text.Text = this.items[position];
ImageView icon = row.FindViewById<ImageView>(Resource.Id.icon);
// ????
return row;
}
}
}
答案 0 :(得分:0)
您似乎正在使用过时的导航抽屉版本。那个人没有开箱即用的图标支持。我建议看詹姆斯的例子:https://github.com/jamesmontemagno/Xam.NavDrawer/tree/master/Design%20Support%20Library%20(Material)/AppCompat%20v14%2B
在那个菜单中,您可以定义菜单,包括以下图标:
<?xml version="1.0" encoding="UTF-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_dashboard"
android:title="Browse" />
<item
android:id="@+id/nav_friends"
android:icon="@drawable/ic_headset"
android:title="Friends" />
<item
android:id="@+id/nav_profile"
android:icon="@drawable/ic_forum"
android:title="Profile" />
</group>
<item android:title="Sub items">
<menu>
<item
android:icon="@drawable/ic_dashboard"
android:title="Sub item 1" />
<item
android:icon="@drawable/ic_forum"
android:title="Sub item 2" />
</menu>
</item>