【问题标题】:Button color from drawable doesn't change来自drawable的按钮颜色不会改变
【发布时间】:2021-07-01 15:00:51
【问题描述】:

我的按钮样式有些问题。我使用 drawable 来改变我的应用按钮的形状和样式。当我在活动 xml 中添加背景时,我的按钮会改变形状但不会改变背景颜色,但是当我使用“setBackgroundResource”方法以编程方式添加背景时,一切正常。 这是我的一段代码:

活动 xml:

`<Button
        android:id="@+id/button_traditional"
        android:background="@drawable/button_default_main_app"
        android:textColor="@color/mustard"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="75dp"
        android:layout_marginEnd="32dp"
        android:onClick="newGame"
        android:text="@string/standard_game"
        app:layout_constraintEnd_toStartOf="@+id/button_extended"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/text_gameChose" />
`

res/drawable:

<solid
    android:color="@color/black"
    />
<corners
    android:topLeftRadius="10dp"
    android:topRightRadius="100dp"
    android:bottomLeftRadius="100dp"
    android:bottomRightRadius="10dp"
    />

提前感谢您的帮助。

编辑: 整个可绘制代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid
        android:color="@color/black"
        />
    <corners
        android:topLeftRadius="10dp"
        android:topRightRadius="100dp"
        android:bottomLeftRadius="100dp"
        android:bottomRightRadius="10dp"
        />

</shape>

【问题讨论】:

  • 发布整个可绘制代码。如果是这样,那么drawable是错误的
  • 好的,我在主帖中进行了编辑。
  • 您的主题也会影响此行为。
  • 好的,所以您建议我的主题也可以进行一些更改,并且可绘制文件不会覆盖主题设置。我应该改变主题?
  • 我已将“Theme.MaterialComponents.DayNight.DarkActionBar”更改为“Theme.AppCompat.DayNight.DarkActionBar”,一切正常。谢谢:)

标签: java android android-layout


【解决方案1】:

默认情况下,按钮色调是原色。需要设置为null,drawable才能通过设置生效。

app:backgroundTint="@null"

所以你的整个按钮应该是这样的:

 <Button
        android:id="@+id/button_traditional"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="75dp"
        android:layout_marginEnd="32dp"
        android:background="@drawable/button_default_main_app"
        android:onClick="newGame"
        android:text="Standard game"
        android:textColor="#a00"
        app:backgroundTint="@null" />

结果:

【讨论】:

  • 你的回答也让我的按钮改变了颜色,一切都很好。谢谢 :) 现在我认为哪个选项更适合我的应用程序。
  • 这个更好,改变你的整个应用主题它没有用
  • 我认为你是对的。感谢你们快速、具体的回答和您的时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-10
  • 1970-01-01
  • 2022-01-02
  • 2023-02-22
  • 2013-09-24
  • 2014-05-17
相关资源
最近更新 更多