按钮不更新的背景 - Android
Background for button not updating - 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>
colors.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>
themes.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>
themes.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/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
因为我还没有定制它,它只是一个新项目
我该如何解决这个问题?
原因:
android:background
没有在按钮中显示正确的颜色,因为在使用 material 组件主题时;应用了默认色调(即它不像以前的 AppCompat
主题那样 null
),它会影响按钮颜色。
解决方案:
您可以通过对按钮应用以下几种方法之一来解决此问题:
- 使用
android:backgroundTint
代替android:background
- 使用
app:backgroundTint="@null"
将色调设置为 null,通常使用 android:background
在我的 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>
colors.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>
themes.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>
themes.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/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
因为我还没有定制它,它只是一个新项目
我该如何解决这个问题?
原因:
android:background
没有在按钮中显示正确的颜色,因为在使用 material 组件主题时;应用了默认色调(即它不像以前的 AppCompat
主题那样 null
),它会影响按钮颜色。
解决方案:
您可以通过对按钮应用以下几种方法之一来解决此问题:
- 使用
android:backgroundTint
代替android:background
- 使用
app:backgroundTint="@null"
将色调设置为 null,通常使用android:background