[英]Different background color for each drawer menu item
I'm developing a native android app with xamarin. 我正在用xamarin开发一个原生的android应用程序。 I need to set a different background color for each item of drawer menu and it should be extended for the whole width of drawer. 我需要为每个抽屉菜单项设置不同的背景颜色,它应该扩展到抽屉的整个宽度。
In this moment, the menu items styles, are set in the drawer activity, in this way: 在这一刻,菜单项样式,在抽屉活动中设置,这样:
itemOrdini = navigationView.Menu.FindItem(Resource.Id.nav_ordini);
Android.Text.SpannableString spanString = new Android.Text.SpannableString(itemOrdini.ToString());
spanString.SetSpan(new Android.Text.Style.ForegroundColorSpan(Resources.GetColor(Resource.Color.rosso_ordini)), 0, spanString.Length(), 0);
itemOrdini.SetTitle(spanString);
notificheOrdini.Gravity = GravityFlags.CenterVertical;
notificheOrdini.Typeface = Typeface.DefaultBold;
notificheOrdini.SetTextColor(Resources.GetColor(Resource.Color.rosso_ordini));
is there a way to achieve the result? 有没有办法实现结果?
You could try to use a custom listview
to instead of the NavigationView
. 您可以尝试使用自定义listview
而不是NavigationView
。 The text color and background color could be set in the adapter's GetView method. 可以在适配器的GetView方法中设置文本颜色和背景颜色。
You could refer to following links for more information: 您可以参考以下链接获取更多信息:
Create a Custom Adapter for Contacts 为联系人创建自定义适配器
For example: 例如:
Main.axml Main.axml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:id="@+id/drawer_layout">
<!-- your content layout -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
MenuList.axml MenuList.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/ItemIcon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp" />
<TextView
android:id="@+id/ItemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp" />
</LinearLayout>
MainActivity.cs MainActivity.cs
public class MainActivity : AppCompatActivity
{
DrawerLayout drawerLayout;
ListView DrawerList;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
drawerLayout = (DrawerLayout)FindViewById(Resource.Id.drawer_layout);
DrawerList = (ListView)FindViewById(Resource.Id.left_drawer);
DrawerList.Adapter =new MenuListAdapter(this);
DrawerList.OnItemClickListener = new onClickListener();
}
}
public class onClickListener :Java.Lang.Object, IOnItemClickListener
{
public void OnItemClick(AdapterView parent, View view, int position, long id)
{
}
}
And the adapter 和适配器
public class MenuListAdapter : BaseAdapter
{
List<MenuItems> Items;
public override int Count => Items.Count;
Activity _activity;
public MenuListAdapter(Activity activity)
{
_activity = activity;
Items = new List<MenuItems>();
Items.Add(new MenuItems() { color = Color.Azure, ItemName = "Home" });
Items.Add(new MenuItems() { color = Color.Green, ItemName = "Home" });
Items.Add(new MenuItems() { color = Color.DarkGoldenrod, ItemName = "Home" });
}
public override Java.Lang.Object GetItem(int position)
{
return Items[position];
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var view = convertView ?? _activity.LayoutInflater.Inflate(
Resource.Layout.MenuList, parent, false);
var Name = view.FindViewById<TextView>(Resource.Id.ItemName);
var Icon = view.FindViewById<ImageView>(Resource.Id.ItemIcon);
Name.Text = Items[position].ItemName;
view.SetBackgroundColor(Items[position].color);
return view;
}
}
class MenuItems : Java.Lang.Object
{
public string ItemName { get; set; }
public Color color { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.