每个抽屉菜单项的不同背景颜色
Different background color for each drawer menu item
我正在使用 xamarin 开发本机 android 应用程序。我需要为抽屉菜单的每个项目设置不同的背景颜色,并且它应该扩展到整个抽屉的宽度。
此时,菜单项样式,设置在抽屉activity,这样:
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));
有没有办法达到这个效果?
您可以尝试使用自定义 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; }
}
我正在使用 xamarin 开发本机 android 应用程序。我需要为抽屉菜单的每个项目设置不同的背景颜色,并且它应该扩展到整个抽屉的宽度。
此时,菜单项样式,设置在抽屉activity,这样:
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));
有没有办法达到这个效果?
您可以尝试使用自定义 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; }
}