在不同状态下使用两个图像切换按钮
16lz
2021-01-22
I need to make a toggle button using two image instead of ON/OFF state.
我需要使用两个图像而不是ON / OFF状态来设置切换按钮。
At off state i set a background image.But the OFF text can not removed while i use background image.
在关闭状态下我设置了背景图像。但是当我使用背景图像时,无法删除OFF文本。
And i can not set another image on ON state by clicking the toggle button :( I am new in android. I hope you guys help me get out of this problem
而且我无法通过点击切换按钮在ON状态下设置另一个图像:(我是android的新手。我希望你们能帮助我摆脱这个问题
3 个解决方案
#1
201
Do this:
做这个:
<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/check" <!--check.xml-->
android:layout_margin="10dp"
android:textOn=""
android:textOff=""
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_centerVertical="true"/>
create check.xml in drawable folder
在drawable文件夹中创建check.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="@drawable/selected_image"
android:state_checked="true" />
<!-- When not selected, use white-->
<item android:drawable="@drawable/unselected_image"
android:state_checked="false"/>
</selector>
更多相关文章
- 如何让按钮共享android中对话框宽度的一半?
- 安卓,如何让两个按钮相邻,宽度相同
- 按下子活动后退按钮后,Android主要活动的元素不响应
- POST json和图像到服务器android
- JSP页面中有一个按钮 点击之后执行JAVA文件
- Java实现图像对比类
- 为可执行文件夹提供自定义图像
- 在扑克游戏中显示卡片的图像
- 不幸的是,在声明按钮时,模拟器中出现了错误