只使用这个,不要使用其他任何东西
<androidx.appcompat.widget.AppCompatButton
<!-- attributes-->
/>
而不是一个
<Button
<!--attributes-->
/>
每件事情都会很好地运作。
愉快的编码。
由于默认的背景着色器颜色,它变为了紫色。 你可以: 改变**app:backgroundTint**而不是android:backgroundColor。在这种情况下,你的背景着色器将代替背景颜色。
或者
添加
app:backgroundTint="@null"
之后您的背景颜色将会显示。
或者
您可以在 Android 清单文件中更改默认主题。例如:
android:theme="@style/Theme.AppCompat"
或 android:theme="@style/Theme.AppCompat.NoActionBar"
最新版的Android Studio默认颜色为紫色。 要更改按钮的颜色,您需要在XML中添加一行代码。
app:backgroundTint="@color/red"
就这些!
app:backgroundTint="@null"
您可能正在使用 targetSdkVersion
30
解决方案:将 theme.xml 样式更改为
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
to
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
从您的按钮XML中删除此内容
android:background="@color/white"
并将其替换为
android:backgroundTint="@color/white"
正如其他人提到的那样,可能是您的主题覆盖了按钮颜色
materialButtonStyle 特别是
您可以
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="MyButtonStyle" parent="TextAppearance.AppCompat.Button" />
<style name="Theme.GradientTest" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="materialButtonStyle">@style/MyButtonStyle</item>
<!-- Other params here. -->
</style>