【问题标题】:Unable to change button background when enable and disable启用和禁用时无法更改按钮背景
【发布时间】:2021-11-12 12:36:03
【问题描述】:

这是我选择的背景:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@drawable/button_background" android:state_enabled="true" />
    <item android:drawable="@drawable/button_disable_background" android:state_enabled="false" />
</selector>

启用背景

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:endColor="#2F98E4"
        android:startColor="#2F98E4"
        android:type="linear" />
    <corners android:radius="20dp"></corners>

</shape>

禁用背景

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:endColor="#2F98E4"
        android:startColor="#10344E"
        android:type="linear" />
    <corners android:radius="20dp"></corners>

</shape>

按钮样式

 <style name="ButtonStyle">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">#ffffff</item>
        <item name="android:textSize">20sp</item>
        <item name="android:gravity">center</item>
        <item name="android:background">@drawable/button_background_selector</item>
        <item name="android:padding">16dp</item>
    </style>

按钮代码

 <TextView
        android:id="@+id/btn_enter_pin_click"
        style="@style/ButtonStyle"
        android:layout_marginTop="@dimen/dp_108"
        android:layout_marginBottom="@dimen/dp_44"
        android:text="@string/done"
        android:enabled="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_enter_pin_preview" />

上面是我尝试启用和禁用按钮时的代码我无法设置按钮的背景我不知道我在做什么错误。

当我运行这段代码时,我得到了:

原因:android.view.InflateException:二进制 XML 文件第 11 行: 二进制 XML 文件第 113 行:膨胀类 TextView 时出错 引起:android.view.InflateException:二进制 XML 文件第 113 行:膨胀类 TextView 时出错 引起:android.content.res.Resources$NotFoundException: Drawable com.anil.gorestapp:drawable/button_background_selector with 资源 ID #0x7f0800d9 引起:android.content.res.Resources$NotFoundException:来自drawable资源ID的文件res/drawable/button_background_selector.xml #0x7f0800d9

请帮帮我

【问题讨论】:

    标签: android kotlin


    【解决方案1】:

    有更好的方法来做到这一点。请注意,除了背景之外,您的两种样式是相同的。相反,制作一个 StateListDrawable 背景,指定启用和禁用状态的背景。然后您可以将其设置为背景,只需启用或禁用带有 setEnabled 的按钮就会自动为您切换它。

    【讨论】:

    • 你能帮帮我吗?实际上我无法设置背景选择,我也尝试过,但没有用。
    • schemas.android.com/apk/res/android"> 。我试过这样
    • 你有 2 个错误,其中一个背景错误。它应该只有 2 项,一项用于启用,一项用于禁用。
    • schemas.android.com/apk/res/android"> 即使保留了两个项目,它也无法正常工作。我得到错误膨胀类 TextView
    • 我改变了问题你能帮我吗
    猜你喜欢
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多