Is it possible to change the color of the line below / Border TextBox (Entry)

I am an Xamarin.Formsapplication Xamarin.Formson Androidand trying to change the color of the line under my Xamarin.Formscontrol Xamarin.Forms Entry.

I have a control Entryas follows:

<Entry Text="new cool street"/>

enter image description here

I would like to change the color of the line under this one Entryfrom white by default to more violet to fit my theme.

Ideally, it would be better to use Android styles, as this applies to all controls inherited from Entryif possible

Is it possible to do this?

+14
source share
8 answers

, ,

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

+31

, colorAccent styles.xml ( Xamarin.Android) Entry .

<item name="colorAccent">#BA55D3</item>
+10

, - . OP Android, Android...

, , . ElementChanged PropertyChanged.

[assembly: ExportRenderer(typeof(Xamarin.Forms.Entry), typeof(CustomEntryRenderer))]
namespace XamFormsConnect.Droid
{
    public class CustomEntryRenderer : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Entry> e)
        {
            base.OnElementChanged(e);

            if (Control != null && e.NewElement != null)
            {
                var entry = (Xamarin.Forms.Entry)e.NewElement;
                if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
                    Control.BackgroundTintList = ColorStateList.ValueOf(entry.TextColor.ToAndroid());
                else
                    Control.Background.SetColorFilter(entry.TextColor.ToAndroid(), PorterDuff.Mode.SrcAtop);
            }
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (e.PropertyName == "TextColor")
            {
                var entry = (Xamarin.Forms.Entry)sender;
                if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
                    Control.BackgroundTintList = ColorStateList.ValueOf(entry.TextColor.ToAndroid());
                else
                    Control.Background.SetColorFilter(entry.TextColor.ToAndroid(), PorterDuff.Mode.SrcAtop);
            }
        }
    }
}
+5

: res/values ​​/colors.xml :   # 303F9F

# 303F9F

<color name="colorPrimaryDark">#303F9F</color>
+3

Xamarin Forms, Mobile.Droid, , "Your Colur", .

0

, , :

<Grid>
   <Entry Placeholder="Your Entry"/>
   <BoxView BackgroundColor="White" HeightRequest="10"/>
</Grid>

, .

0

If someone is experiencing a malfunction of width or y with the iOS root user renderer, I found a solution.

  1. Include a record and two more variables as class fields:
private CustomEntry _entry;
private double _yPos;
private double _width;
  1. Assign a value to OnElementChanged:
if (e.NewElement != null)
    _entry = e.NewElement as CustomEntry;
  1. In OnElementPropertyChangedinclude the following:
if (e.PropertyName == "Width")
{
    _width = _entry.Width;
}
else if (e.PropertyName == "Height")
{
    _yPos = _entry.Height;
}
_line.Frame = new CGRect(0, _yPos, _width, 1f);
0
source

All Articles