我正在Android
上创建一个Xamarin.Forms
应用程序,并试图更改Xamarin.Forms
Entry
控件下面的线条的颜色。
我有一个这样的Entry
控件:
<Entry Text="new cool street"/>
我想将这个Entry
下面的线条的颜色从默认的白色改为更多的紫色,以匹配我的主题。
理想情况下,最好使用Android Styles,因为如果可能的话,它将应用于所有从Entry
继承的控件
这有可能做到吗?
发布于 2016-07-06 23:00:44
您可以使用将影响所有条目的自定义渲染器,
这是针对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解决方案
发布于 2017-09-06 10:16:11
由于我的内容页面具有一种背景颜色,而对话框具有另一种背景颜色,因此使用Styles来指定底部栏颜色是完全错误的答案。既然操作人员只问了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);
}
}
}
}
发布于 2016-07-05 23:36:30
简单:将res/values/colors.xml编辑为:#303F9F
您可以在of#303F9F中放置任何十六进制颜色代码
<color name="colorPrimaryDark">#303F9F</color>
https://stackoverflow.com/questions/38207168
复制相似问题