我在为自定义按钮着色时遇到问题。由于某种原因,似乎无论我想应用什么颜色更改(文本或背景),按钮都保持不变。
我注意到button.xml
具有所需的颜色和正确的形状,尽管没有出现按钮background-color
活动的财产
活动中的按钮
<Button
android:id="@+id/button2"
android:layout_width="162dp"
android:layout_height="53dp"
android:background="@drawable/button"
android:text="@string/button"
android:textAllCaps="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.132"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/messagTextView"
app:layout_constraintVertical_bias="0.804" />
自定义按钮形状
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="50sp"/>
<solid android:color="@color/redAdobeXD"/>
</shape>
如果您正在使用材料成分 https://developer.android.com/guide/topics/ui/look-and-feel(如果这是一个相对较新的项目,则可能是这种情况)然后您设计Button
用另一种方式:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".FirstFragment">
<Button
android:id="@+id/button"
style="@style/Widget.MaterialComponents.Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="32dp"
android:text="I am a Button!"
android:textColor="@color/white"
app:backgroundTint="#FF0000"
app:cornerRadius="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
需要注意的事项有:
style="@style/Widget.MaterialComponents.Button"
android:textColor="@color/white"
-
app:backgroundTint="#FF0000"
(将会@color/redAdobeXD
在你的情况下)
app:cornerRadius="50dp"
不需要额外的 xml 并在按钮等上进行设置(仅在更多自定义情况下)。
您可以阅读有关可用选项的更多信息包含按钮 here https://material.io/develop/android/components/buttons#contained-button
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)