Android:如何将选择器与单个项目一起使用

发布于 2025-01-27 18:47:35 字数 1259 浏览 6 评论 0原文

我有一个带有文本的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文