Xamarin-从CustomRenderer类更改时间选择器的颜色
如何从CustomRender更改计时器的颜色 这是我的自定义渲染器类,但不起作用Xamarin-从CustomRenderer类更改时间选择器的颜色,xamarin,xamarin.android,Xamarin,Xamarin.android,如何从CustomRender更改计时器的颜色 这是我的自定义渲染器类,但不起作用 public class CustomTimePickerRenderer : TimePickerRenderer { protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.TimePicker> e) { base.OnElementChanged(e);
public class CustomTimePickerRenderer : TimePickerRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.TimePicker> e)
{
base.OnElementChanged(e);
if(Control != null)
{
Control.SetTextColor(Android.Graphics.Color.Red);
Control.SetBackgroundColor(Android.Graphics.Color.Red);
}
}
}
公共类CustomTimePickerRenderer:TimePickerRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(控件!=null)
{
Control.SetTextColor(Android.Graphics.Color.Red);
SetBackgroundColor(Android.Graphics.Color.Red);
}
}
}
您想更改什么颜色?在自定义渲染器中,只需更改控件的颜色(在本机android平台中实现EditText
,请参阅)
下面是TimePicker的一些颜色更改,在Android项目的资源/值/样式中创建主题.picker
样式
<style name="Theme.picker" parent="Theme.AppCompat.Light.Dialog">
//background color
<item name="android:background">#FFC107</item>
//Title background color
<item name="colorAccent">#FF0000</item>
//text color
<item name="android:textColor">@android:color/white</item>
//edittext color
<item name="android:textColorPrimary">@android:color/holo_green_light</item>
</style>
效果如下:
谢谢你的回答。我想动态更改此颜色,但styles.xml不会以编程方式更改。这就是我提出此问题的原因。如果要动态更改其颜色,需要创建自定义时间选择器对话框,并使用反射设置NumberPicker的属性。我们通常在样式中配置其颜色。我更改了UI,不再需要时间选择器对话框
<style name="MainTheme" parent="MainTheme.Base">
</style>
<!-- Base theme applied no matter what API -->
<style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
...
<item name="android:timePickerDialogTheme">@style/Theme.picker</item>
</style>