Android按钮的背景颜色未更改

77

在这个Android项目中,我正在创建一个默认的按钮样式。 我的styles.xml文件

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:buttonStyle">@style/ButtonStlye123</item>
</style>

<style name="ButtonStlye123" parent="android:style/Widget.Button">
    <item name="android:textSize">19dp</item>
    <item name="android:layout_margin">8dp</item>
    <item name="android:padding">8dp</item>
    <item name="android:textColor">@color/ColorDivider</item>

    <item name="android:background">@color/ColorAccent</item>
</style>

我的按钮在一个片段中。

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btnLogin"
    android:id="@+id/btn_login"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:theme="@style/ButtonStlye123"
    android:onClick="login" />
在Android Studio的预览中,它看起来和我想要的一样,但是当我在我的设备上运行应用程序时,背景颜色是灰色(“默认按钮颜色”)。如果我像这样在styles.xml中更改文本颜色:
<item name="android:textColor">@color/ColorPrimary</item>

即使在我的设备上也发生了变化,因此自定义样式正在加载。我尝试了不同的颜色(对于文本有效),但按钮的颜色不会改变。

为什么我的背景颜色没有加载?


你确定样式名称“buttonstyle123”和你的按钮“questbuttonstyle”不应该相同吗? - alltooconfusingthereforesleep
@alltooconfusingthereforesleep 是的,它们应该是一样的,在这里放置时出现了拼写错误。在我的应用程序中它们是相同的。 - Sven van den Boogaart
2
尝试使用android:style="@style/ButtonStlye123"(而不是“theme”) - Booger
1
样式是为组件设计的,主题是为应用程序(或全局属性)设计的。它们在概念上是不同的(不确定它们在执行上是否不同)。 - Booger
更改背景色调 - mahdi
显示剩余2条评论
11个回答

234

尝试使用AppCompactButton而不是

<Button>

使用

<androidx.appcompat.widget.AppCompatButton>

那样就行了

更新:2021年01月06日

发现以上方法在早期的安卓版本上不起作用。 对于Material Design按钮,请使用

<Button app:backgroundTint="@color/green">

3
有趣的是,即使我直接在按钮上设置“android:background”,也无法生效。但对于AppCompatButton则有效。因此,我猜测这与AppCompatActivity和简单Activity的样式设置方式有关...不管怎样,这个解决方案对于我(使用AppCompatActivity)有效。 - Dmitrii Semikin
4
可以。已生效,但我不明白其背后的原因。 - Aman
3
似乎对于一个按钮(Button),应用程序主题中定义的主要颜色优先于您想要应用于按钮的任何自定义背景,但这不适用于AppCompatButton。不确定这是Android的错误还是特性。 - ceyquem
4
背景色调对我有用,谢谢伙计。 - Mr. Disability
1
工作得很好,我想我们正在逐渐从Button转向AppCompatButton - vin shaba
显示剩余6条评论

52

请使用 androidx.appcompat.widget.AppCompatButton 替代 Button

  <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/button_id_Login"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/textInnputLayout_editText_id_password"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="@string/Login_screen_button_text"
        android:background="@drawable/login_button_style"
        android:textAllCaps="false">
    </androidx.appcompat.widget.AppCompatButton>

2
这个解决了我的问题...不要使用>按钮。 - Maduro
2
@Maduro 另外,要么停止使用 Material Theme,要么您也可以设置app:backgroundTint="@null" - EpicPandaForce

48

你可能正在使用targetSdkVersion 30的Material主题

解决方案是更改theme.xml样式

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

2
这真是救了我的命。我花了三天时间来寻找问题。谢谢你。 - hmartinezd
1
谢谢,但为什么会发生这种情况? - Web.11
2
@Web.11:由于最新版本的Android Studio自动使用Material主题,因此按钮应用了这些主题组件。另一种方法是使用androidx.appcompat.widget.AppCompatButton,或将父级主题更改为Theme.AppCompat.Light.NoActionBar。 - Hor Chanpheng

28
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">

更换为

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

将MaterialComponents更改为AppCompat对我有效。


16

我认为你需要将属性从“theme”更改为“style”。 这些文件的用途不同,因此使用方式也不同。

样式用于为单个视图或组件分配属性,而主题用于将属性应用于整个应用程序。

所以请尝试这个:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btnLogin"
    android:id="@+id/btn_login"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    <!-- This attribute was changed -->        
    android:style="@style/ButtonStlye123"
    android:onClick="login" />

适当地将这些不同类型的XML拆分到正确的文件中(例如styles.xml和themes.xml - 而不是将它们全部保存在同一个文件中)。 这是传统做法,不会影响代码的编译或执行,但建议作为代码风格。


是否可以自动应用于所有按钮? - Sven van den Boogaart
如果您想要普遍地应用样式,您需要创建一个Java类(自定义),该类扩展Button类,然后在该Java类中分配样式。否则,没有内置的方法使某个特定样式普遍适用。您可以在样式中放置许多属性(包括宽度、高度等),因此这是集中处理此类内容的一种方式(再次强调,您需要手动在XML中放置_style_属性,无法自动执行此操作)。 - Booger
我正在尝试这个https://dev59.com/F3E95IYBdhLWcg3wN7Kv解决方案,但我无法让它工作。 - Sven van den Boogaart
我从未使用过这个。我认为只需在XML中将样式属性应用于每个视图即可(不需要普遍应用)。 - Booger

7

使用backgroundTint属性会有所帮助,它对我很有效。例如:

android:backgroundTint="@color/md_orange_800"

当使用可绘制对象作为背景时,这种方法效果不佳,因此更好的选择是改用AppCompatButton,如此在这里提到的https://dev59.com/VlwZ5IYBdhLWcg3wGs_D#66950268。 - Loveth

6

你只需要在styles.xml文件中将你的应用程序主题从Theme.MaterialComponents.DayNight.DarkActionBar更改为Theme.AppCompat.Light.NoActionBar

例如:

从:style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar"

到:style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"


6
如果您正在使用Android Studio 4.1及以上版本(Beta),请检查位于values文件夹中的themes.xml文件。
<style name="Theme.NavaFlashLightV4" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
</style>

将属性更改为 @null

<style name="Theme.NavaFlashLightV4" parent="Theme.MaterialComponents.DayNight.NoActionBar">
            <!-- Primary brand color. -->
            <item name="colorPrimary">@null</item> <!-- HERE -->
            <item name="colorPrimaryVariant">@color/purple_700</item>
            <item name="colorOnPrimary">@color/white</item>
    </style>

对于其他旧版本的Android Studio

只需更改此属性

<item name="colorPrimary">@color/colorPrimary</item>

to

        <item name="colorPrimary">@color/@null</item>

最终将按钮的背景设置为所需的可绘制图标。

例子:

<Button
        android:id="@+id/btnSwitch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/ic_launcher_background" /> <!--the drawable you want-->

将主要颜色设置为null会导致在Android Studio 4.2上应用程序崩溃。 - Andrew S
我认为,如果你想正确地为你的应用程序进行样式设计,特别是按钮,你需要停止使用MaterialComponents样式,因为它们会将按钮替换为appCompat等效项,从而引起复杂性。 - Andrew S

0

我正在使用targetSdk 30和compileSdk 32,并且我正在使用Button小部件。Android Studio版本为2021.1.1

在我的情况下,以下更改有效:

在AndroidManifest.xml中,在“application”标签下将“android:theme”从

android:theme="@style/Theme.AppCompat.Light.NoActionBar"

android:theme="@style/Theme.MaterialComponents.DayNight.NoActionBar"

我使用了Theme.AppCompat.Light.NoActionBar来去除标题栏。刚开始我尝试在themes.xml中将“style”更改为“Theme.AppCompat.Light.NoActionBar”,但这对我没用。

0

你的风格应该是这样的...

<style name="ButtonStlye123" >
    <item name="android:textSize">19dp</item>
    <item name="android:layout_margin">8dp</item>
    <item name="android:padding">8dp</item>
    <item name="android:textColor">@color/ColorDivider</item>

    <item name="android:background">@color/ColorAccent</item>
</style>

并且在布局中

<Button
     android:id="@+id/btn3"
     style="@style/ButtonStlye123"
     android:layout_width="@dimen/sixzero"
     android:layout_height="@dimen/sixzero"
     android:text="3" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接