从Xamarin表单的条目控制中删除下划线

时间:2019-09-25 13:41:12

标签: xamarin xamarin.forms

我是Xamarin-Forms的新手,他正在开发登录表单并使用Material Design(IVisual)。我创建了一个自定义的Entry类,并使用MaterialEntryRenderer继承了它来对其进行自定义。 我要实现的是删除下划线Entry有。我看到了很多示例,但所有示例都使用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

4 个答案:

答案 0 :(得分:1)

尝试一下,它对我有用

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

答案 1 :(得分:0)

尝试更改“文本颜色”属性。

根据来源,它会更改您想要的内容:

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

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

Xamarin 安卓

条目

[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;
            }
        }
    }
}
相关问题