shape文件用法一:在Android中,用XML文件来设置颜色的渐变
2014-04-30 14:50
781 查看
需求:要完成颜色的渐变。
做法:使用 xml文件,结合 shape完成
————————————————————————————————————————————
设置颜色的渐变,可以通过res/drawable里定义的一个xml 完成,如:/TestColor/res/drawable/color_shape.xml
写法儿如下:
shape是用来定义形状的,gradient定义该形状里面为渐变色填充,startColor起始颜色,endColor结束颜色,angle表示方向角度。当angle=0时,渐变色是从左向右。 然后逆时针方向转,当angle=90时为从下往上。
Shape文件实现颜色渐变在项目中的使用方法:
实现过程:
第一步:在 res/drawable/目录下定义 xml文件:
/TestColor/res/drawable/color_shape.xml
第二步:在项目布局文件中,加入引用/TestColor/res/layout/activity_main.xml
第三步:在代码中调用 activity_main 即可:
这便是一个完整的,使用 shape 完成控制颜色渐变的实例。
效果图 如下:
做法:使用 xml文件,结合 shape完成
————————————————————————————————————————————
设置颜色的渐变,可以通过res/drawable里定义的一个xml 完成,如:/TestColor/res/drawable/color_shape.xml
写法儿如下:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <!-- startColor : 设置渐变颜色的开始值 endColor: 设置渐变颜色的结束值 angle : 设置渐变的角度 90 :从下往上开始渐变 0 :从左往右开始渐变 --> <gradient android:startColor="#FFF" android:endColor="#030" android:angle="90" /> </shape>
shape是用来定义形状的,gradient定义该形状里面为渐变色填充,startColor起始颜色,endColor结束颜色,angle表示方向角度。当angle=0时,渐变色是从左向右。 然后逆时针方向转,当angle=90时为从下往上。
Shape文件实现颜色渐变在项目中的使用方法:
实现过程:
第一步:在 res/drawable/目录下定义 xml文件:
/TestColor/res/drawable/color_shape.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <!-- startColor : 设置渐变颜色的开始值 endColor: 设置渐变颜色的结束值 angle : 设置渐变的角度 90 :从下往上开始渐变 0 :从左往右开始渐变 --> <gradient android:startColor="#FFF" android:endColor="#030" android:angle="90" /> </shape>
第二步:在项目布局文件中,加入引用/TestColor/res/layout/activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/color_shape" tools:context=".MainActivity" > </RelativeLayout>
第三步:在代码中调用 activity_main 即可:
package com.zhangeng.testcolor; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
这便是一个完整的,使用 shape 完成控制颜色渐变的实例。
效果图 如下:
相关文章推荐
- shape文件用法一:在Android中,用XML文件来设置颜色的渐变
- 【android】 使用一个shape.xml文件,使用代码设置不同圆角背景颜色
- shape文件用法二:在Android中,用XML文件实现圆角的效果
- Android Shape资源文件gradient颜色渐变方向
- Android GradientDrawable类的详解,设置activity的背景颜色渐变效果
- Android GradientDrawable类的详解,设置activity的背景颜色渐变效果
- android使用shape画背景阴影、背景颜色渐变
- 【Android 界面效果20】Android GradientDrawable类的详解,设置activity的背景颜色渐变效果
- Android GradientDrawable类的详解,设置activity的背景颜色渐变效果
- Android中xml布局文件中使用include引入布局进行复用(解决使用include布局重叠,颜色设置无效问题)
- 设置android的布局文件的背景颜色为黑色
- Android开发在string.xml文件中设置部分字体颜色大小
- Android GradientDrawable类的详解,设置activity的背景颜色渐变效果
- Android GradientDrawable类的详解,设置activity的背景颜色渐变效果
- Android GradientDrawable类的详解,设置activity的背景颜色渐变效果
- 1.android 改变状态栏的颜色,2 设置透明渐变式的样式!,设置状态栏为透明的渐变的
- android 的渐变背景设置文件
- android在xml文件中实现背景或按钮颜色渐变
- Android GradientDrawable类的详解,设置activity的背景颜色渐变效果[整理后的源码]
- Android编程设置TextView颜色setTextColor用法实例