【问题标题】:How to Change Checkbox/Radibutton Color in Xamarin?如何更改 Xamarin 中的复选框/单选按钮颜色?
【发布时间】:2018-01-31 12:18:36
【问题描述】:

我已经使用自定义呈现在我的应用程序中实现了单选按钮和复选框控件。我通过参考以下链接呈现了控件。控件默认颜色为白色。由于我的应用程序背景颜色是白色,因此控件不可见。我试图改变控件的颜色。但它不起作用。文字颜色只会改变。谁能建议如何更改复选框/单选按钮控件的颜色?

https://github.com/XLabs/Xamarin-Forms-Labs

提前致谢。

【问题讨论】:

    标签: xamarin xamarin.android xamarin.forms


    【解决方案1】:

    您可以在 OnElementChanged 方法中使用以下行代码从渲染器设置 Xamarin.Forms 控件(如 Switch)颜色:

    Control.OnTintColor = Color.Blue.ToUIColor();
    

    对于您的 XLab 控件,您需要在 CheckBoxRenderer 中设置背景颜色并在 OnElementChanged 方法中更改颜色。或者你也可以从控制中改变。

    BackgroundColor = Element.BackgroundColor.ToUIColor();
    

    对于 Android,您可以通过以下材料设计进行管理:

    用这个创建一个新的 style.xml 文件(将 colorAccent 中的颜色更改为您喜欢的任何颜色)并将其放入 Resources 文件夹中。

    <?xml version="1.0" encoding="UTF-8"?>
    <resources>
        <style name="MyTheme" parent="MyTheme.Base">
        </style>
        <!-- Base theme applied no matter what API -->
        <style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- colorAccent is used as the default value for colorControlActivated which is used to tint widgets -->
            <item name="colorAccent">#0000FF</item>
            <!-- You can also set colorControlNormal, colorControlActivated colorControlHighlight and colorSwitchThumbNormal. -->
        </style>
    </resources>
    

    然后将属性添加到 MainActivity.cs 以加载该主题。

    [Activity(Label = "MyApp", Icon = "@drawable/icon", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, WindowSoftInputMode = SoftInput.AdjustResize)]
    public class MainActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-21
      • 2018-01-24
      • 2017-09-21
      • 2013-04-19
      • 2018-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-20
      相关资源
      最近更新 更多