繁体   English   中英

Android更改切换图像

[英]Android change switch image

我想知道如何更改android中的开关图像。 我知道我们可以自定义开关并改变颜色等,但我们如何使用另一个图像作为开关。 我尝试从我的drawables设置背景但开关仍然可见并显示在我的图像上。 我知道我们可以使用切换按钮或图像按钮但是我很好奇我们是否可以这样做到开关。

在Switch组件中覆盖android:thumb和android:track以覆盖switch images和backgroundsl。 例如:

<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:thumb="@drawable/drawable_switch"
android:track="@drawable/drawable_bg" />

其中drawable_switch和drawable_bg分别是选择器。

drawable_switch.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="@drawable/switch_disabled" />
<item android:state_pressed="true"  android:drawable="@drawable/switch_pressed" />
<item android:state_checked="true"  android:drawable="@drawable/switch_enabled" />
<item android:drawable="@drawable/switch_default" />

drawable_bg.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="@drawable/switch_bg_disabled" />
<item android:state_focused="true"  android:drawable="@drawable/switch_bg_focused" />
<item android:drawable="@drawable/switch_bg_default" />

@Jerry尝试这个,如果你还有任何问题还原

android:thumb属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM