Xamarin表单-设置开关文本颜色 [英] Xamarin forms - set switch text color

查看:58
本文介绍了Xamarin表单-设置开关文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在android渲染器中设置文本的颜色?我有以下渲染器:

How can I set the color of the text in an android renderer? I have the following renderer:

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似乎不起作用.

I am able to change the color of the switch, but I can't figure out how to change the color of the YES/NO text. SetTextColor doesn't seem to work.

推荐答案

在droid项目中的Resources \ values目录下创建一个xml文件. 名称无关紧要,但需要以.xml结尾并包含

Create an xml file under the Resources\values directory in your droid project. doesn't matter the name but it needs to end in .xml and contain

<?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.您可以从"Resources"下名为"Resource.designer.cs"的生成文件中获取ID.或者,您可以调用如下生成的Const.

Then in your renderer call Control.SetSwitchTextAppearance. Pass in the Context and the ResId of the resource you created. You can get the Id from the generated file under Resources titled Resource.designer.cs. Alternately you can call the Const that generated as follows.

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

希望它会有所帮助.如果您无法获取,请告诉我我上载了一个示例应用程序.

Hope it helps. If you cant get it let me know Ill upload a sample app.

这篇关于Xamarin表单-设置开关文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆