每个抽屉菜单项的不同背景颜色

时间:2018-03-26 17:56:16

标签: android xamarin colors background menuitem

我正在使用xamarin开发一个原生的Android应用程序。我需要为每个抽屉菜单项设置不同的背景颜色,并且应该扩展到抽屉的整个宽度。

enter image description here

此时,菜单项样式在抽屉活动中设置,以这种方式:

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

有没有办法实现这个结果?

1 个答案:

答案 0 :(得分:1)

您可以尝试使用自定义listview代替NavigationView。 可以在适配器的GetView方法中设置文本颜色和背景颜色。

您可以参考以下链接获取更多信息:
Create a Custom Adapter for Contacts

例如:
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

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

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

    }
}

和适配器

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