可绘制的按钮颜色不会改变
Button color from drawable doesn't change
我的按钮样式有问题。我使用 drawable 来改变我的应用程序按钮的形状和样式。当我在 activity xml 中添加背景时,我的按钮会改变形状但不会改变背景颜色,但是当我使用“setBackgroundResource”方法以编程方式添加背景时,一切正常。
这是我的一段代码:
activity 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需要设置为null才能生效
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" />
结果:
我的按钮样式有问题。我使用 drawable 来改变我的应用程序按钮的形状和样式。当我在 activity xml 中添加背景时,我的按钮会改变形状但不会改变背景颜色,但是当我使用“setBackgroundResource”方法以编程方式添加背景时,一切正常。 这是我的一段代码:
activity 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需要设置为null才能生效
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" />
结果: