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

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

问题描述

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

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,

这是针对android:

here's for android:

[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

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

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