Xamarin Forms安卓端:修改MORE菜单弹出标签页的背景与文字颜色
解决Xamarin Forms安卓TabBar MORE菜单标签页背景与文字颜色问题
一、通过自定义渲染器直接修改Overflow菜单样式
在安卓项目中创建TabBar的自定义渲染器,直接操作原生BottomNavigationView来设置MORE菜单的背景和文字颜色:
- 在安卓项目下新建
Renderers文件夹,创建CustomTabBarRenderer.cs文件,代码如下:
using Android.Content; using Android.Support.Design.Widget; using Android.Graphics; using Xamarin.Forms; using Xamarin.Forms.Platform.Android; [assembly: ExportRenderer(typeof(TabbedPage), typeof(YourAppNamespace.Droid.Renderers.CustomTabBarRenderer))] namespace YourAppNamespace.Droid.Renderers { public class CustomTabBarRenderer : TabbedPageRenderer { public CustomTabBarRenderer(Context context) : base(context) { } protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e) { base.OnElementChanged(e); if (Control == null || e.NewElement == null) return; var bottomNav = (BottomNavigationView)Control; // 设置Overflow菜单背景色 bottomNav.SetBackgroundColor(Color.ParseColor("#2c3e50")); // 替换为你的目标背景色 // 设置菜单文字颜色(区分启用/禁用状态) var colorState = new ColorStateList( new int[][] { new int[] { Android.Resource.Attribute.StateEnabled }, new int[] { -Android.Resource.Attribute.StateEnabled } }, new int[] { Color.White, // 启用状态文字颜色 Color.Gray // 禁用状态文字颜色 }); var menu = bottomNav.Menu; for (int i = 0; i < menu.Size(); i++) { menu.GetItem(i).SetTitleTextColor(colorState); } } } }
二、通过安卓样式文件全局配置(更彻底)
如果渲染器方式不够,可通过安卓资源样式全局定义Overflow菜单样式:
- 在
Resources/values/styles.xml中添加自定义样式:
<style name="CustomBottomNavStyle" parent="@style/Widget.Design.BottomNavigationView"> <item name="android:popupBackground">#2c3e50</item> <!-- 菜单背景色 --> <item name="android:textColor">@color/tab_text_selector</item> <!-- 文字颜色状态列表 --> </style>
- 在
Resources/values/colors.xml中创建文字颜色状态选择器:
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="#FFFFFF" android:state_checked="true"/> <item android:color="#FFFFFF" android:state_enabled="true"/> <item android:color="#808080" android:state_enabled="false"/> </selector> <color name="tab_text_selector">@drawable/tab_text_color_selector</color>
- 在
MainActivity.cs中应用该样式:
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // 应用自定义BottomNav样式 SetTheme(Resource.Style.CustomBottomNavStyle); Xamarin.Essentials.Platform.Init(this, savedInstanceState); global::Xamarin.Forms.Forms.Init(this, savedInstanceState); LoadApplication(new App()); }
注意事项
- 替换代码中的
YourAppNamespace为你的应用实际命名空间 - 所有颜色值可根据需求替换为目标十六进制色值或资源引用
- 确保安卓项目已引用
Xamarin.Android.Support.DesignNuGet包
内容的提问来源于stack exchange,提问作者Ramkesh Banwala
相关产品推荐
相关产品推荐

