我不知道为什么这段代码不起作用。任何人都可以帮忙吗?我试图让按钮使用名为“greenstart”和“greenend”的自定义颜色。颜色已在 res/value/string.xml 文件中创建。我看过类似的问题,但要么没有答案,要么不清楚,要么解决方案不起作用。提前致谢。
XML 文件样本:
<Button
android:id="@+id/mycollection"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<Gradient
android:angle="270"
android:endColor="@color/greenstart"
android:startColor="@color/greenend" >
</Gradient>
</Button>
最佳答案
创建一个新的xml文件并将其放入drawable中,然后将其作为背景添加到按钮中
gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- Gradient Bg for listrow -->
<gradient
android:startColor="#f1f1f2"
android:centerColor="#e7e7e8"
android:endColor="#cfcfcf"
android:angle="270" />
</shape>
layout.xml
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@drawable/gradient"
android:text="Übernehmen" >
关于android - 如何通过xml为android中的按钮添加渐变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12166559/