按钮背景未更新 - Andorid
在我的 Android 项目中,我有一个背景为紫色的按钮,但它显示为蓝色。
这是我的代码:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:gravity="center"
android:background="@drawable/background_gradient"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="50dp"
android:background="@color/blue">
<Button
android:id="@+id/Button_StartGame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/purple"
android:layout_margin="10dp"
android:text="@string/start_game" />
</RelativeLayout>
</LinearLayout>
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="lawnGreen">#7CFC00</color>
<color name="neonGreen">#39FF14</color>
<color name="cyan">#00FFFF</color>
<color name="blue">#0000FF</color>
<color name="yellow">#FFFF00</color>
<color name="purple">#800080</color>
<color name="pink">#EE82EE</color>
<color name="black">#000000</color>
<color name="white">#FFFFFF</color>
</resources>
theme.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.MultiSmart" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/blue</item>
<item name="colorPrimaryVariant">@color/purple</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/blue</item>
<item name="colorSecondaryVariant">@color/purple</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
theme.xml (night)
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.MultiSmart" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/blue</item>
<item name="colorPrimaryVariant">@color/purple</item>
<item name="colorOnPrimary">@color/black</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/blue</item>
<item name="colorSecondaryVariant">@color/purple</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
我没有提供 MainActivity.java
因为我还没有自定义它,这只是一个新项目。
我该如何解决这个问题?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
原因:
android:background
在按钮中没有显示正确的颜色,因为使用材质组件主题时;应用了默认色调(即它不是像之前的 AppCompat 主题那样的 null ),并且它会影响按钮颜色。解决方案:
您可以通过对按钮应用以下几种方法之一来解决此问题:
android:backgroundTint
而不是android:background
将色调设置为 null app:backgroundTint="@null"
,通常使用android:background
Cause:
The
android:background
doesn't show up the right color in the button because when using a material components theme; there is a default tint color applied (i.e. it's notnull
like in the previousAppCompat
themes), and it affects the button color.Solution:
You can solve this by applying one of a couple of ways to the button:
android:backgroundTint
instead ofandroid:background
app:backgroundTint="@null"
, and normally useandroid:background