android-UI组件实例大全(一)------TextView文本框

来源:互联网 发布:淘宝投诉怎么撤销 编辑:程序博客网 时间:2024/06/05 06:57

小知识点:

dp(dip): device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA、HVGA和QVGA 推荐使用这个,不依赖像素。

px: pixels(像素). 不同设备显示效果相同,一般我们HVGA代表320x480像素,这个用的比较多。

pt: point,是一个标准的长度单位,1pt=1/72英寸,用于印刷业,非常简单易用;

sp: scaled pixels(放大像素). 主要用于字体显示best for textsize。

文本框(TextView)


TestView代码实例:

1.对电话与邮件增加链接:

代码:

[html] view plaincopyprint?
  1. <span style="font-family:Comic Sans MS"> <!-- 对电话与邮件增加链接 -->  
  2.     <TextView  
  3.         android:layout_width="wrap_content"  
  4.         android:layout_height="wrap_content"  
  5.         android:singleLine="true"  
  6.         android:text="手机号码12345678821,邮箱地址779878443@qq.com"  
  7.         android:autoLink="phone|email"  
  8.    /></span>  

运行截图:


代码解释:

singleLine:设置单行显示,如果一行显示不完的话,就用...表示后面的文本

autoLink:将指定文本转换为可单击的超链接形式,属性值有:none,map,phone,email.all,web;可写成上面那种形式,两种超链接类型




2.定义一个带图片的TextView,字体大小为20pt,且图片在文本上方

代码:

[html] view plaincopyprint?
  1. <span style="font-family:Comic Sans MS"><!-- 定义一个带图片的TextView,且图片在文本上方 -->  
  2.     <TextView  
  3.         android:textSize="20pt"  
  4.         android:layout_width="wrap_content"  
  5.         android:layout_height="wrap_content"   
  6.         android:text="我是一个带图片的TextView"  
  7.         android:drawableTop="@drawable/ic_launcher"  
  8.     /></span>  

运行截图:


代码解释:

textSize:设置TextView显示文本的大小

drawableTop:设置图片的位置,这里的话我们设置为文字上面,当然你也可以设置他在左右或者底部改下drawableLeft这样就可以了




3.设置文本的颜色,并使用阴影效果:

这里要注意一点:阴影效果在adt上是不显示的,需要运行在avd上才能看到效果哦!

代码:

[html] view plaincopyprint?
  1. <!-- 设置文字颜色,使用阴影效果 -->  
  2.     <TextView  
  3.         android:textSize="20pt"  
  4.         android:layout_width="wrap_content"  
  5.         android:layout_height="wrap_content"   
  6.         android:textColor="#841271"  
  7.         android:text="我有阴影....."  
  8.         android:shadowColor="#567841"  
  9.         android:shadowRadius="3.0"  
  10.         android:shadowDx="10.0"  
  11.         android:shadowDy="10.0"        
  12.     />  

运行截图:



代码解释:

textColor:设置文本的颜色

shadowColor:指定文本的阴影颜色,需要与shadowRadius一起使用

shadowRadius:设置阴影的模糊程度,设为0.1就变成字体颜色了,建议使用3.0

shadowDx:设置阴影在水平方向的偏移,就是水平方向阴影开始的横坐标位置

shadowDy:设置阴影在竖直方向的偏移,就是竖直方向阴影开始的纵坐标位置





4.定义一个边框的TextView

我们都知道textview并没有边框,如果要实现有边框的textview只能用背景了

代码:

[html] view plaincopyprint?
  1. <!-- 带边框的TextView -->  
  2.     <TextView  
  3.         android:textSize="20pt"  
  4.         android:layout_width="wrap_content"  
  5.         android:layout_height="wrap_content"   
  6.         android:text="带边框的文本..."  
  7.         android:background="@drawable/border"      
  8.     />  

border.xml是建立在drawable的一个xml文件,用于绘制边框

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.       
  4.     <!-- 设置一个蓝色边框 -->  
  5.     <stroke android:width="4px" android:color="#07387B"/>  
  6.     <!-- 制定渐变背景色,使用sweep类型的渐变 -->  
  7.     <gradient   
  8.         android:startColor="#81D33F"  
  9.         android:centerColor="#FCA974"  
  10.         android:endColor="#F80000"  
  11.         android:type="sweep"  
  12.     />  
  13. </shape>  

运行截图:



代码分析:

background:设置textview的背景

[html] view plaincopyprint?
  1. android:width:设置边框的粗细,这里设为4px,四个像素点  


shape优化界面详解见:

http://tech.ddvip.com/2012-10/1351226528184208.html




5.定义圆角边框的TextView

代码:

[html] view plaincopyprint?
  1. <!-- 带圆角边框的TextView -->  
  2.    <TextView  
  3.        android:textSize="20pt"  
  4.        android:layout_width="wrap_content"  
  5.        android:layout_height="wrap_content"   
  6.        android:text="圆角边框"  
  7.        android:background="@drawable/border"      
  8.    />  

border.xml文件:背景图片

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <!-- 设置透明背景色 -->  
  4.     <solid android:color="#000"/>  
  5.       
  6.     <!-- 设置一个绿色边框 -->  
  7.     <stroke android:width="4px" android:color="#00FF00"/>  
  8.     <!-- 设置四个圆角的半径 -->  
  9.     <corners   
  10.         android:topLeftRadius="20px"  
  11.         android:topRightRadius="10px"  
  12.         android:bottomRightRadius="20px"  
  13.         android:bottomLeftRadius="10px"  
  14.     />  
  15. </shape>  


运行截图:



代码解释:

这个很简单,注释很详细地介绍了这里就略过了










0 0
原创粉丝点击