当切换和禁用的android开关全部变灰时 [英] When Toggled and disabled android switch is all greyed out

查看:174
本文介绍了当切换和禁用的android开关全部变灰时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如上所示,当禁用iOS中的开关切换并且切换具有颜色时,而在android中则没有. 可以在android中实现相同的效果吗?

As seen above when the switch toggle in iOS is disabled and toggled has color, but in the android doesnt. Is possible to do the same effect in android?

推荐答案

如果只想更改拇指颜色以匹配iOS UISwitch轨道绿色(#41D150),则可以在Resources/values/style.xml文件(在您的Xamarin.Android项目中):

If you just want to change the thumb color to match the iOS UISwitch track green color (#41D150), you can change the colorSwitchThumbNormal in the Resources/values/style.xml file (in your Xamarin.Android project):

添加:

<item name="colorSwitchThumbNormal">#41D150</item>

API 19-API 26上运行的表单:

当开关被禁用并被切换时,我希望不透明度降低

when the switch is disabled and is toggled, I want less opacity

如果要更改track颜色,可以使用非常简单的渲染器来更改Android上所有Forms的Switch:

If you want to change the track color, you can use a very simple renderer that changes all the Forms' Switch on Android:

[assembly: ExportRenderer(typeof(Switch), typeof(StyleBasedSwitchRenderer))]
namespace Forms_PCL_XAML.Droid
{
    public class StyleBasedSwitchRenderer : SwitchRenderer
    {
        protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Switch> e)
        {
            base.OnElementChanged(e);
            Control?.SetTrackResource(Resource.Drawable.form_switch_track_mtrl_alpha);
        }
    }
}

Resource.Drawable.form_switch_track_mtrl_alpha是一个9Patch图像. normal 主题在AppCompat库中使用abc_switch_track_mtrl_alpha.9.png.

The Resource.Drawable.form_switch_track_mtrl_alpha is a 9Patch image. the normal theme uses abc_switch_track_mtrl_alpha.9.png in the AppCompat library.

因此,如果您添加类似的9Patch但更改颜色:

So if you add a similar 9Patch but change the color:

您最终得到:

或者使用基于可绘制的选择器和两个9Patch图像来保持相同的拇指默认颜色:

Or use a drawable-based selector and two 9Patch images to maintain the same default colors of the thumbs:

渲染器变为:

Control?.SetTrackResource(Resource.Drawable.form_switch_selector);

Resource.Drawable文件夹中的

选择器(form_switch_selector.xml):

Selector (form_switch_selector.xml) in the Resource.Drawable folder:

<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_checked="true" android:drawable="@drawable/form_switch_track_selected" /> 
     <item android:drawable="@drawable/form_switch_track_mtrl_alpha" /> 
 </selector>

两个9Patch文件:

The two 9Patch files:

结果:

这篇关于当切换和禁用的android开关全部变灰时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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