.NET MAUI中Entry控件无法显示下划线的问题求助
解决.NET MAUI Entry在Android上不显示下划线的问题
方案1:修改Android全局主题样式
打开Android项目下的Resources\values\styles.xml文件,修改应用主题,指定带下划线的EditText样式:
<style name="MainTheme" parent="MainTheme.Base"> <item name="editTextStyle">@style/UnderlinedEditText</item> </style> <style name="UnderlinedEditText" parent="@android:style/Widget.EditText"> <item name="android:background">@android:drawable/edit_text</item> </style>
此设置会让所有Entry控件默认显示下划线,适合全局统一样式的需求。
方案2:通过Handler单独配置Entry(灵活可控)
在页面后台代码中,针对Android平台为Entry配置Handler,启用下划线:
using Microsoft.Maui.Handlers; using Android.Widget; public MainPage() { InitializeComponent(); #if ANDROID // 全局配置所有Entry EntryHandler.Mapper.AppendToMapping("AddUnderline", (handler, view) => { if (handler.PlatformView is EditText editText) { editText.Background = Android.App.Application.Context.GetDrawable(Android.Resource.Drawable.EditText); // 可选:自定义下划线颜色 // editText.BackgroundTintList = Android.Content.Res.ColorStateList.ValueOf(Android.Graphics.Color.Gray); } }); // 仅针对当前页面的entry控件配置 entry.HandlerChanged += (s, e) => { if (entry.Handler?.PlatformView is EditText editText) { editText.Background = Android.App.Application.Context.GetDrawable(Android.Resource.Drawable.EditText); } }; #endif }
方案3:跨平台模拟下划线(不受系统限制)
如果需要跨平台统一样式,或者自定义下划线的粗细、颜色,可以用Border控件包裹Entry来模拟:
<Border Margin="10,2,10,2" Padding="0" StrokeThickness="0,0,0,1" StrokeColor="Black" BackgroundColor="Transparent"> <Entry x:Name="entry" Placeholder="Enter text" TextChanged="entry_TextChanged" Completed="entry_Completed" PlaceholderColor="Black" BackgroundColor="Transparent"/> </Border>
这种方式完全自定义,不受Android系统主题影响,同时在iOS等平台也能保持一致效果。
内容的提问来源于stack exchange,提问作者user1837641
相关产品推荐
相关产品推荐

