【问题标题】:Remove underline from Entry Control in Xamarin Forms从 Xamarin 表单中的条目控件中删除下划线
【发布时间】:2022-01-20 16:17:48
【问题描述】:

我是 Xamarin-Forms 的新手,正在开发登录表单并使用 Material Design (IVisual)。我创建了一个自定义的 Entry 类并使用MaterialEntryRenderer 继承它来自定义它。 我想要实现的是删除下划线Entry have。我看过很多例子,但他们都使用EntryRenderer

public class CustomEntryRenderer : MaterialEntryRenderer
{
    public CustomEntryRenderer(Context context) : base(context) { }

    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if (Control != null)
        {
           Control.Background = null;
           Control.SetBackgroundColor(Android.Graphics.Color.Transparent);
        }
    }
}

它适用于EntryRenderer,但不适用于MaterialEntryRenderer

【问题讨论】:

  • 您可以使用Effect 来执行此操作,请阅读链接中的Why Use an Effect over a Custom Renderer? 部分
  • 从 2020 年开始,可以尝试 Xamarin 社区工具包中的 RemoveBorderEffect。

标签: xamarin xamarin.forms


【解决方案1】:

试试这个,它对我有用

Control.EditText.Background = null;
Control.EditText.SetBackgroundColor(Android.Graphics.Color.Transparent);

【讨论】:

    【解决方案2】:

    创建效果并将其应用到控件,我刚刚写了一篇详细的文章,介绍如何通过简单的步骤做到这一点:How to remove android entry underline

    【讨论】:

    • 也许您可以将文章的某些部分复制粘贴到您的答案中,以使您的答案不言自明,而无需点击链接。
    • 完美运行。谢谢!
    • 也适用于 Entry、Picker 和 Editor :)
    • 推荐,开箱即用,半打代码。
    【解决方案3】:

    在新版本的 Xamarin Forms Visual Material 4.8+ 中,我使用以下代码,它在 Android 和 iOS 上运行良好。

    Xamarin Android

    条目

    [assembly: ExportRenderer(typeof(Entry), typeof(EntryMaterialRendererAndroid), new[] { typeof(VisualMarker.MaterialVisual) })]
    namespace XFTest.Droid.Renderers
    {
        public class EntryMaterialRendererAndroid : MaterialEntryRenderer
        {
            public EntryMaterialRendererAndroid(Context context) : base(context) { }
            protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
            {
                base.OnElementChanged(e);
    
                if (Control != null)
                {
                    Control.BoxStrokeWidth = 0;
                    Control.BoxStrokeWidthFocused = 0;
                }
            }
        }
    }
    

    Xamarin iOS

    条目

    [assembly: ExportRenderer(typeof(Entry), typeof(EntryMaterialRendereriOS), new[] { typeof(VisualMarker.MaterialVisual) })]
    namespace XFTest.iOS.Renderers
    {
        public class EntryMaterialRendereriOS : MaterialEntryRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
            {
                base.OnElementChanged(e);
    
                EntryRemoveUnderLine();
            }
    
            protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
            {
                base.OnElementPropertyChanged(sender, e);
    
                EntryRemoveUnderLine();
            }
    
            protected void EntryRemoveUnderLine()
            {
                if (Control != null)
                {
                    Control.BorderStyle = UITextBorderStyle.None;
                    Control.Underline.Enabled = false;
                    Control.Underline.DisabledColor = UIColor.Clear;
                    Control.Underline.Color = UIColor.Clear;
                    Control.Underline.BackgroundColor = UIColor.Clear;
                    // Remove underline on focus
                    Control.ActiveTextInputController.UnderlineHeightActive = 0f;
                    // Remove placeholder background
                    Control.PlaceholderLabel.BackgroundColor = UIColor.Clear;
                }
            }
        }
    }
    

    【讨论】:

      【解决方案4】:

      我正在使用 Xamarin Form 5 版本 (5.0.0.2291)

      你可以关注这个

      https://kudzaidube.medium.com/xamarin-forms-how-to-remove-android-entry-underline-4628a8d1f058

      只需更改这一行

      Android.Graphics.Color entryLineColor = Android.Graphics.Color.White;

      Android.Graphics.Color entryLineColor = new Android.Graphics.Color(0,0,0,0);

      为了使用透明色

      【讨论】:

        【解决方案5】:

        尝试更改文本颜色属性。

        根据来源,它会改变你想要的:

        _textInputLayout.BoxBackgroundColor = MaterialColors.CreateEntryFilledInputBackgroundColor(Element.BackgroundColor, Element.TextColor);
        

        看:https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Material.Android/MaterialEntryRenderer.cs

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-09
          • 2019-04-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-08-12
          相关资源
          最近更新 更多