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

查看:146
本文介绍了如何通过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中,然后将其添加为按钮作为背景

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天全站免登陆