[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上的解决方案不太确定。
我遇到了同样的问题,只需更改 styles.xml
文件(在 Xamarin.Android 项目中)中的 colorAccent
值,即可更改 Entry
字段的光标颜色和底部边框的颜色。
<item name="colorAccent">#BA55D3</item>
由于我的内容页面和对话框使用不同的背景颜色,使用样式来指定底部栏颜色完全是错误的答案。而且由于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);
}
}
}
}
我尝试了最受欢迎的解决方案,但出现错误:“自版本2.5起,上下文已过时。请改用本地上下文。”
经过一些快速搜索:
代码需要更新:
using <YOUR_APP_NAME>.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Android.Content;
using Android.OS;
using Android.Content.Res;
using Android.Graphics;
[assembly: ExportRenderer(typeof(Entry), typeof(MyRenderers))]
namespace Android.MyRenderers
{
public class MyRenderers : EntryRenderer
{
public MyRenderers(Context context) : base(Android.App.Application.Context)
{
}
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(Android.Graphics.Color.Ivory);
else
Control.Background.SetColorFilter(Android.Graphics.Color.Ivory, PorterDuff.Mode.SrcAtop);
}
}
}
为需要更新的人提供更新。
简单易懂的方法: 在res/values/colors.xml中编辑如下内容: #303F9F
你可以将任何十六进制颜色代码替换#303F9F。
<color name="colorPrimaryDark">#303F9F</color>
如果有人在使用root的iOS自定义渲染器时遇到宽度或y位置出现问题,我找到了解决方法。
private CustomEntry _entry;
private double _yPos;
private double _width;
OnElementChanged
中分配数值:if (e.NewElement != null)
_entry = e.NewElement as CustomEntry;
OnElementPropertyChanged
中,包含以下内容:if (e.PropertyName == "Width")
{
_width = _entry.Width;
}
else if (e.PropertyName == "Height")
{
_yPos = _entry.Height;
}
_line.Frame = new CGRect(0, _yPos, _width, 1f);
在Android中将输入光标和下划线颜色从粉色更改为其他颜色。只需在Xamarin Forms Android项目的Resources/values文件夹中添加<item name="colorControlActivated">#007bff</item>
到styles.xml文件中即可轻松实现。
注意:此方法将应用更改到应用程序中的每个元素。如果不需要这样,请使用自定义渲染器方法。
可以参考xamarin论坛上的答案。
上面的解决方案已经过时了。这个是推荐的解决方案。(同时包含所有必要的使用方法。有时可能会令人困惑...)
using System;
using System.ComponentModel;
using Android.Content;
using Android.Content.Res;
using InteriorCircle.Droid.CustomComp;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(Entry), typeof(CustomEntry))]
namespace InteriorCircle.Droid.CustomComp
{
public class CustomEntry : EntryRenderer
{
public CustomEntry(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control == null || e.NewElement == null) return;
Control.BackgroundTintList = Android.Content.Res.ColorStateList.ValueOf(Android.Graphics.Color.Black);
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == Entry.TextProperty.PropertyName)
{
if (Control.Text.Length > 6) //this is your condition(For example, here is the length of the text content)
{
Control.BackgroundTintList = ColorStateList.ValueOf(Android.Graphics.Color.Red);
}
else
{
Control.BackgroundTintList = ColorStateList.ValueOf(Android.Graphics.Color.Black);
}
}
}
}
}
另一个简单的可视化解决方案是隐藏这行代码:
<Grid>
<Entry Placeholder="Your Entry"/>
<BoxView BackgroundColor="White" HeightRequest="10"/>
</Grid>
您可以创建自己的组件,而非使用硬编码颜色来改进它。
<item name="colorAccent">@android:color/yourcolour</item>
- Akash AminTextBox
没有焦点时,colorAccent
不起作用。我想在TextBox
失去焦点时改变行颜色。 - JKennedy