2015-10-19 53 views
1

如何设置android渲染器中文本的颜色?我有以下渲染:Xamarin窗体 - 设置切换文本颜色

public class CustomSwitchRenderer : SwitchRenderer 
    { 
     protected override void OnElementChanged(ElementChangedEventArgs<Switch> e) 
    { 
     base.OnElementChanged(e); 

     if (Control != null) 
     { 
      Control.TextOn = "Yes"; 
      Control.TextOff = "No"; 

      Android.Graphics.Color colorOn = Android.Graphics.Color.Rgb(239, 201, 6); 
      Android.Graphics.Color colorOff = Android.Graphics.Color.LightGray; 
      Android.Graphics.Color colorDisabled = Android.Graphics.Color.Gray; 
      Android.Graphics.Color textColor = Android.Graphics.Color.Black; 

      Control.SetTextColor (ColorStateList.ValueOf (textColor)); 
      Control.SetTextColor (textColor); 

      StateListDrawable drawable = new StateListDrawable(); 
      drawable.AddState(new int[] { Android.Resource.Attribute.StateChecked }, new ColorDrawable(colorOn)); 
      drawable.AddState(new int[] { -Android.Resource.Attribute.StateEnabled }, new ColorDrawable(colorDisabled)); 
      drawable.AddState(new int[] { }, new ColorDrawable(colorOff)); 

      Control.ThumbDrawable = drawable; 

     } 
    } 
} 

我能够改变开关的颜色,但我无法弄清楚如何改变YES/NO文本的颜色。 SetTextColor似乎不起作用。

回答

4

在您的droid项目的Resources \ values目录下创建一个xml文件。 不要紧的名字,但它需要以.xml结束,并包含

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="CodeFont" parent="@android:style/TextAppearance.Medium"> 
     <item name="android:textColor">#00FF00</item> 
    </style> 
</resources> 

然后在您的通话渲染Control.SetSwitchTextAppearance。传入您创建的资源的上下文和ResId。您可以从Resource.designer.cs下的资源下的生成文件中获取Id。或者,您可以调用生成的Const,如下所示。

Control.SetSwitchTextAppearance (Control.Context, Resource.Style.CodeFont); 

希望它有帮助。如果你不能得到它,让我知道我上传一个示例应用程序。

+0

嗨,你可以上传与iOS和Droid自定义渲染示例应用程序。 – sudheer