Mar*_*ina 6 java android button imagebutton android-xml
我有一个按钮,提供了这样的颜色,图像和文本:
android:background="@color/green"
android:drawableLeft="@drawable/custom_routes_start_button_icon"
android:text="@string/custom_route_start"
Run Code Online (Sandbox Code Playgroud)
这是未选中的状态,希望选中的状态是这样的:
android:background="@color/red"
android:drawableLeft="@drawable/custom_routes_stop_button_icon"
android:text="@string/custom_route_stop"
Run Code Online (Sandbox Code Playgroud)
就我所知,不可能在选择器中为文本或drawableLeft(仅可绘制)提供一个项目。有人知道实现此目标的好方法吗?也许另一个te选择器也可以引用的xml文件?
我不确定 ToggleButton(甚至复选框)是否适合您的用例。您确实可以为您的可绘制对象(左、右、上、下)使用选择器,使用带有选择器的背景,甚至为您的两个状态使用不同的文本。
实际上,您可以为您的背景定义一个选择器 (/res/color/custom_color.xml)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/green"
android:state_checked="true" />
<item android:color="@color/red"
android:state_checked="false"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
一个用于 drawableLeft(/res/drawable/custom_drawable.xml)。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/custom_routes_start_button_icon"
android:state_checked="true" />
<item android:drawable="@color/custom_routes_stop_button_icon"
android:state_checked="false"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
最后你的 ToggleButton 定义为
android:background="@color/custom_color"
android:drawableLeft="@drawable/custom_drawable"
android:textOn="@string/custom_route_start"
android:textOff="@string/custom_route_stop"
Run Code Online (Sandbox Code Playgroud)
您可能必须使用样式才能使其显示为您想要的样式。
虽然这是一个迟到的答案,但希望有人觉得它有用。
您应该使用两个按钮并且只显示其中之一。android:visibility在 XML 中使用并setVisibility()显示/隐藏按钮。
因此,首先使开始按钮可见并隐藏停止按钮。当用户按下开始按钮时,隐藏开始按钮并显示停止按钮。当用户按下停止按钮时,将其隐藏并再次显示开始按钮。
| 归档时间: |
|
| 查看次数: |
4092 次 |
| 最近记录: |