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

来源:互联网 发布:淘宝追加评论语 编辑:程序博客网 时间:2024/06/05 07:29

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

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

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

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

写法儿如下:

[html] view plaincopyprint?在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 plaincopyprint?在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 plaincopyprint?在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 plaincopyprint?在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
原创粉丝点击