android - 如何通过xml为android中的按钮添加渐变?

标签 android xml user-interface button colors

我不知道为什么这段代码不起作用。任何人都可以帮忙吗?我试图让按钮使用名为“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/

相关文章:

android - 在 PreferenceActivity 中膨胀复选框时出错

java - GUI 在 Java 的 While 循环期间卡住

java - Android sqlite - "No such table"而表存在

android - 如何在 gradle 中生成 Dagger (v1) 对象图 (*.dot)?

java - 无法正确退出或完成我的应用程序 android( Activity )

java - 如何删除 JLabel 上的边框?

Java-获取选定列表项的值

android - 服务没有按应有的方式唤醒设备

php - XML 解析 - 缺少节点

python - 如何删除XML文件中的重复元素