如何通过xml为android中的按钮添加渐变? [英] How to add a gradient to buttons in android through xml?

查看:26
本文介绍了如何通过xml为android中的按钮添加渐变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚为什么此代码不起作用.任何人都可以帮忙吗?我试图让按钮使用名为greenstart"和greenend"的自定义颜色.颜色已在 res/value/string.xml 文件中创建.我看过类似的问题,但要么没有回答,要么不清楚,要么解决方案不起作用.提前致谢.

I cannot figure out why this code will not work. Can anyone help? I am trying to get the button to use a custom color titled 'greenstart' and 'greenend'. The colors have been created in the res/value/string.xml file. I have looked at similar questions but either they were left unanswered, unclear, or the solution did not work. Thanks in advance.

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中,然后添加到button作为背景

Create a new xml file and put it in drawable and then add it to button as background

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" >

这篇关于如何通过xml为android中的按钮添加渐变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆