gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 13:49:53 26 4
gpt4 key购买 nike

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

enter image description here

此时,菜单项的样式,在drawer 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

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

菜单列表.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>

主 Activity .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; }
}

关于android - 每个抽屉菜单项的不同背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49497703/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com