Android Studio XML按钮背景色未更改

ev7lccsx  于 2023-01-26  发布在  Android
关注(0)|答案(2)|浏览(180)

我有这个XML标签:

<Button
        android:id="@+id/next"
        android:layout_width="305dp"
        android:layout_height="64dp"
        android:background="@color/colorTheme"
        style="@style/ButtonStyle"
        android:text="Next"
        app:layout_constraintBottom_toTopOf="@+id/imageViewFooterGetStarted"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.968" />

由于某种原因,按钮的背景颜色没有改变。我尝试只使用style.xml:

<style name="ButtonStyle" parent="android:Widget.Button">
        <item name="android:background">@drawable/btn_get_started</item>
        <item name="android:textColor">@color/colorTheme</item>
    </style>

并且仍然是默认的紫色。android:background也没有任何效果。是什么原因导致背景颜色没有改变?
编辑:我找到了解决方案。原来android:background改变了颜色,但只有在使用<androidx.appcompat.widget.AppCompatButton>标签而不是<button>标签的情况下

mbyulnm0

mbyulnm01#

尝试使用AppCompatButtom而不是按钮
将按钮替换为AppCompatButtom

<androidx.appcompat.widget.AppCompatButton
            android:id="@+id/next"
            android:layout_width="305dp"
            android:layout_height="64dp"
            android:background="@color/colorTheme"
            style="@style/ButtonStyle"
            android:text="Next"
            app:layout_constraintBottom_toTopOf="@+id/imageViewFooterGetStarted"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.968" />

希望它能起作用!

ppcbkaq5

ppcbkaq52#

screenshot替换<Button... />为<androidx.appcompat.widget.AppCompatButton... />Activity_main.xml代码中的

相关问题