是否可以更改文本框(条目)下方/边框的线条颜色 [英] Is it possible to change the colour of the line below / Border of a TextBox (Entry)

查看:30
本文介绍了是否可以更改文本框(条目)下方/边框的线条颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Android 上创建一个 Xamarin.Forms 应用程序,并且我正在尝试更改我的 Xamarin.Forms 下方行的颜色入口控件.

I am creating a Xamarin.Forms application on Android and I am trying to change the colour of the line below my Xamarin.Forms Entry control.

我有一个 Entry 控件,如下所示:

I have an Entry control like so:

<Entry Text="new cool street"/>

我想将此 Entry 下方的线条颜色从默认的白色更改为更多的紫色以匹配我的主题.

I would like to change the colour of the line below this Entry from the default white to more of a purple to match my theme.

理想情况下,最好使用 Android 样式,因为它适用于所有从 Entry 继承的控件

Idealy it would be better to do using Android Styles as it would apply to all controls inheriting from Entry if possible

这可以吗?

推荐答案

您可以使用将影响所有条目的自定义渲染器,

you can use custom renderer that will affect all entries,

适用于安卓:

[assembly: ExportRenderer(typeof(Entry), typeof(MyEntryRenderer))]
namespace Android.MyRenderers
{
    public class MyEntryRenderer : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if (Control == null || e.NewElement == null) return;

            if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
                Control.BackgroundTintList = ColorStateList.ValueOf(Color.White);
            else
                Control.Background.SetColorFilter(Color.White, PorterDuff.Mode.SrcAtop);
         }    
    }
}

和 iOS:

[assembly: ExportRenderer (typeof(Entry), typeof(MyEntryRenderer))]
namespace iOS.MyRenderers
{
    public class MyEntryRenderer : EntryRenderer
    {
        private CALayer _line;

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

            if (Control == null || e.NewElement == null)
                return;

            Control.BorderStyle = UITextBorderStyle.None;

            _line = new CALayer {
                BorderColor = UIColor.FromRGB(174, 174, 174).CGColor,
                BackgroundColor = UIColor.FromRGB(174, 174, 174).CGColor,
                Frame = new CGRect (0, Frame.Height / 2, Frame.Width * 2, 1f)
            };

            Control.Layer.AddSublayer (_line);
        }
    }
}

不确定有关此的 Windows 解决方案

not sure about Windows solution on this

这篇关于是否可以更改文本框(条目)下方/边框的线条颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆