Android:如何将选择器与单个项目一起使用
我有一个带有文本的togglebutton,只有在选择它的情况下,我想显示旁边的dablablestart。当没有选择时,我想仅显示文本(可绘制的应消失)。
我尝试将选择器仅使用一个项目来设置android:state_checked = true
时,但是当我运行应用程序时,它将被完全忽略。我该怎么做?
这是我的代码togglebutton代码:
<ToggleButton
android:id="@+id/list_item_service_list_add_button"
style="@style/OrangeToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:textOff="@string/list_item_service_list_add"
android:textOn="@string/list_item_service_list_added"
android:button="@android:color/transparent"
android:drawableStart="@drawable/add_to_cart_icon_selector"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
这是我的add_to_cart_icon_selector.xml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/ic_check_circle"
android:state_checked="true" />
</selector>
I have a ToggleButton with text and I want to show a drawableStart next to it only if it's selected. When it isn't selected, I'd like to show just the text (the drawable should disappear).
I tried setting up my selector with just a single item for when android:state_checked=true
but when I run the app it gets completely ignored. How can I do this?
Here's my code ToggleButton code:
<ToggleButton
android:id="@+id/list_item_service_list_add_button"
style="@style/OrangeToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:textOff="@string/list_item_service_list_add"
android:textOn="@string/list_item_service_list_added"
android:button="@android:color/transparent"
android:drawableStart="@drawable/add_to_cart_icon_selector"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
and here's my add_to_cart_icon_selector.xml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/ic_check_circle"
android:state_checked="true" />
</selector>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论