shape文件用法一:在Android中,用XML文件来设置颜色的渐变

需求:要完成颜色的渐变。

做法:使用 xml文件,结合 shape完成

————————————————————————————————————————————

设置颜色的渐变,可以通过res/drawable里定义的一个xml 完成,如:/TestColor/res/drawable/color_shape.xml

写法儿如下:

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <!--   
  4.         startColor : 设置渐变颜色的开始值  
  5.         endColor: 设置渐变颜色的结束值  
  6.           
  7.         angle : 设置渐变的角度  
  8.             90 :从下往上开始渐变  
  9.             0 :从左往右开始渐变  
  10.      -->  
  11.     <gradient   
  12.         android:startColor="#FFF"  
  13.         android:endColor="#030"  
  14.         android:angle="90"     
  15.         />  
  16.   
  17. </shape>  

shape是用来定义形状的,gradient定义该形状里面为渐变色填充,startColor起始颜色,endColor结束颜色,angle表示方向角度。当angle=0时,渐变色是从左向右。 然后逆时针方向转,当angle=90时为从下往上。

 

Shape文件实现颜色渐变在项目中的使用方法:

 

实现过程:

 

第一步:在 res/drawable/目录下定义 xml文件:

/TestColor/res/drawable/color_shape.xml

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <!--   
  4.         startColor : 设置渐变颜色的开始值  
  5.         endColor: 设置渐变颜色的结束值  
  6.           
  7.         angle : 设置渐变的角度  
  8.             90 :从下往上开始渐变  
  9.             0 :从左往右开始渐变  
  10.      -->  
  11.     <gradient   
  12.         android:startColor="#FFF"  
  13.         android:endColor="#030"  
  14.         android:angle="90"     
  15.         />  
  16.   
  17. </shape>  

第二步:在项目布局文件中,加入引用/TestColor/res/layout/activity_main.xml

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.       
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.       
  7.     android:background="@drawable/color_shape"  
  8.       
  9.     tools:context=".MainActivity" >  
  10.       
  11. </RelativeLayout>  

第三步:在代码中调用 activity_main 即可:

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.zhangeng.testcolor;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6.   
  7. public class MainActivity extends Activity {  
  8.   
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.activity_main);  
  13.     }  
  14.   
  15.     @Override  
  16.     public boolean onCreateOptionsMenu(Menu menu) {  
  17.         // Inflate the menu; this adds items to the action bar if it is present.  
  18.         getMenuInflater().inflate(R.menu.main, menu);  
  19.         return true;  
  20.     }  
  21.   
  22. }  


这便是一个完整的,使用 shape 完成控制颜色渐变的实例。

效果图 如下:


原文出自:http://blog.csdn.net/watermusicyes/article/details/24791253


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值