【问题标题】:Android Button background color not changingAndroid按钮背景颜色没有改变
【发布时间】:2015-10-29 18:08:35
【问题描述】:

在这个 android 项目中,我正在创建一个默认按钮样式。 我的样式.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 是的,他们应该,把它放在这里时打错了。它们在我的应用中是一样的
  • TRY: android:style="@style/ButtonStlye123" (而不是“主题”)
  • 样式用于组件,主题用于应用(或全局属性)。它们在概念上不同(不确定它们在执行上是否不同)
  • @Booger 谢谢它成功了!

标签: android


【解决方案1】:

尝试使用 AppCompactButton 而不是

<Button

使用

<androidx.appcompat.widget.AppCompatButton

这样就可以了

更新:2021 年 1 月 6 日

发现上述方法不适用于早期的 Android 版本。 用于材料设计按钮使用

app:backgroundTint="@color/green"

【讨论】:

  • 有趣的是,对我来说,即使直接在 Button 上设置“android:background”也不起作用。但它适用于 AppCompatButton。所以我想,这与 AppCompatActivity 和简单 Activity 的样式有关......无论如何,这个解决方案对我有用(使用 AppCompatActivity)。
  • 成功了。但我不明白这背后的原因。
  • 我发现这个问题现在很常见。从来不知道更改为AppCompatButton 会解决它。虽然这并不是针对原始问题的真正答案,但它现在肯定对我们有所帮助。
  • 似乎对于 Button,应用程序主题中定义的原色优先于您要应用于 Button 的任何自定义背景,但 AppCompatButton 并非如此。不确定这是错误还是 Android 的功能
  • backdroundTint 为我工作,谢谢伙计。
【解决方案2】:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">

替换为

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

将 MaterialComponents 更改为 AppCompat 对我有用..

【讨论】:

    【解决方案3】:

    请使用 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>
    

    【讨论】:

    • 这个解决了我的问题...不要使用>按钮
    【解决方案4】:

    我认为您需要将属性从“主题”更改为“样式”。这些文件用于不同的事物,因此使用方式不同。

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

    所以试试这个吧:

    <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 和 theme.xml - 而不是将它们都保存在同一个文件中)。这是约定俗成的,不会影响代码编译或执行,但推荐作为代码风格。

    【讨论】:

    • 是否可以自动应用到所有按钮?
    • 如果您想通用地应用一种样式,您需要创建一个 Java 类(您自己定制的),它扩展了 Button 类,然后在该 Java 类中分配样式。否则,没有内置方法可以使某种样式普遍适用。您可以在样式中放置很多属性(包括宽度、高度等),因此这是集中这些东西的方法(同样,您需要手动将 style 属性放入 XML 中(没办法自动执行此操作)
    • 我正在尝试这个stackoverflow.com/questions/2410836/… 解决方案,但我无法让它工作
    • 我从来没有用过这个。我认为只需将样式属性应用于 XML 中的每个视图就很容易了(不觉得需要普遍应用)。
    【解决方案5】:

    您可能正在使用targetSdkVersion 30 材质主题

    解决办法是改变theme.xml的风格

    来自

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

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

    【讨论】:

    • 这救了我的命。就像3天试图找到问题一样。谢谢。
    • @hmartinezd:不客气^^
    【解决方案6】:

    如果您使用的是 Android Studio 4.1 及更高版本(测试版) 检查你的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>
    

            <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-->
    

    【讨论】:

      【解决方案7】:

      您只需在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"

      【讨论】:

        【解决方案8】:

        使用这个属性backgroundTint它会帮助你。它对我有用。示例:

        android:backgroundTint="@color/md_orange_800"
        

        【讨论】:

        【解决方案9】:

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

        <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" />
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-09-09
          • 1970-01-01
          • 2015-10-25
          • 2021-04-08
          • 1970-01-01
          • 2021-07-05
          • 2013-08-06
          • 2017-03-25
          相关资源
          最近更新 更多