shape文件用法一:在Android中,用XML文件来设置颜色的渐变
2015-06-16 10:44
447 查看
需求:要完成颜色的渐变。
做法:使用 xml文件,结合 shape完成
————————————————————————————————————————————
设置颜色的渐变,可以通过res/drawable里定义的一个xml 完成,如:/TestColor/res/drawable/color_shape.xml
写法儿如下:
[html] view
plaincopyprint?
<?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
[html] view
plaincopyprint?
<?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
[html] view
plaincopyprint?
<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 即可:
[java] view
plaincopyprint?
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 完成控制颜色渐变的实例。
效果图 如下:
原文出自:http://blog.csdn.net/watermusicyes/article/details/24791253
做法:使用 xml文件,结合 shape完成
————————————————————————————————————————————
设置颜色的渐变,可以通过res/drawable里定义的一个xml 完成,如:/TestColor/res/drawable/color_shape.xml
写法儿如下:
[html] view
plaincopyprint?
<?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
[html] view
plaincopyprint?
<?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
[html] view
plaincopyprint?
<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 即可:
[java] view
plaincopyprint?
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 完成控制颜色渐变的实例。
效果图 如下:
原文出自:http://blog.csdn.net/watermusicyes/article/details/24791253
相关文章推荐
- android 监听系统时区变化,日期变化,时间变化
- Android设备各种使用尺寸整理
- android闹钟实现原理
- android自动化测试解决跨进程通信问题
- android按钮旋转简单实现
- Android webView 实现与JS
- [Android] AS 中 Gradle 配置运行浅析
- [Android] 环境配置之Android Studio开发NDK
- Android Studio Rendering Problems不能显示xxx.xml布局文件
- Android:控件AutoCompleteTextView 自动提示
- android sdk 原版 百度盘分享
- android studio 更新 Gradle错误解决方法
- Android程序退出,杀掉进程
- Android内存优化之封装九宫格
- Android应用Activity、Dialog、PopWindow、Toast窗口添加机制及源码分析
- Android Studio 导.so文件简单、高效的方法
- Android中使用Handler造成内存泄露的分析和解决
- Android使用ViewPager实现滚动广告解决OOM小技巧
- Android getevent用法
- Android 学习笔记之五大布局