Android开发总结笔记 TextView(文本框) 1-1-6

来源:互联网 发布:管家婆软件怎么退货 编辑:程序博客网 时间:2024/06/13 11:58

TextView的继承结构



文本框就是一个显示文字信息的组件,简单易用,但是如果结合上一些关于样式的类各种Span
可以做出一些不错的样式和效果,并且方便对样式进行管理。


在API(TextView API)文档中TextView有一个xml属性是放在前面说的

android:textIsSelectable="true"
这个属性是设置文本是否可选,设置为true之后,长按TextView会触发系统内置的复制和粘贴功能。




下面展示一下一些常用属性的用法
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:text="I am a textview!"
android:textSize="40dp"
android:background="@android:color/black"
android:gravity="center"
android:textStyle="bold"
android:textColor="@android:color/holo_blue_bright"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>



  • android:text    文本的内容
  • android:textSize    文字的大小
  • android:textStyle   文字的样式,此处有三个属性值   normal(无效果)  bold(粗体)   italic(斜体)  对应Java中的setTypeBuffer方法,
  • android:textColor    文字的颜色
  • android:background    文本框的背景色
  • android:gravity    文本内容的对齐方式 




关于阴影的属性
  • android:shadowColor    设置阴影颜色,需要和android:Radius一起配合使用
  • android:shadowRadius    设置阴影的模糊程度(据说设置为3.0比较好)
  • android:shadowDx    设置阴影在水平方向的偏移
  • android:shadowDy    设置阴影在垂直方向的偏移

下面是代码实现
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
 
<TextView
android:text="I am a textview!"
android:textSize="40dp"
android:background="@android:color/black"
android:gravity="center"
android:shadowColor="@android:color/holo_orange_dark"
android:shadowRadius="3.0"
android:shadowDx="10.0"
android:shadowDy="10.0"
android:textStyle="bold"
android:textColor="@android:color/holo_blue_bright"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>




带有图片的TextView
在TextView的API中,有四个可以为TextView添加图片的属性
  • android:drawableTop    在TextView的顶部添加图片
  • android:drawableBottom    在TextView的底部添加图片
  • android:drawableLeft    在TextView的左边添加图片
  • android:drawableRight    在TextView的右边添加图片

下面就用代码来看一下效果
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
 
<TextView
android:text="I am a textview!"
android:textSize="40dp"
android:gravity="center"
android:drawableLeft="@mipmap/ic_launcher"
android:drawableBottom="@mipmap/ic_launcher"
android:drawableRight="@mipmap/ic_launcher"
android:drawableTop="@mipmap/ic_launcher"
android:textColor="@android:color/holo_green_dark"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

效果图




发现问题
通过这样的方法添加的图片好像不能调整大小,那就只能通过Java代码实现了

TextView textView = (TextView) findViewById(R.id.text);
Drawable[] drawables = textView.getCompoundDrawables();
//顺序是,左上右下
drawables[1].setBounds(0, 50, 200, 200);
textView.setCompoundDrawables(drawables[0],drawables[1],drawables[2],drawables[3]);



setBounds这个方法的意思就是
比如说这句:
drawables[1].setBounds(0,50,200,200);

长度是:从文字的左边0dp开始,到200dp处
宽度是:从文字的上方的50dp向上延伸200dp


android:autoLink识别链接类型

  • android:autoLink="phone"    手机号
  • android:autoLink="web"    网络链接
  • android:autoLink="mail"    邮箱链接
  • android:autoLink="map"    地图链接
  • android:autoLink="all"    全部
  • android:autoLink="non"   无

设置为all的时候可以不写协议头,autolink会自动识别
但是要为TextView设置setMovementMethod(LinkMovementMethod.getInstance())这个方法,不然没有效果


支持HTML标签的TextView
除了以上功能之外,TextView还支持默认支持以下html标签。
  • <a href="...">
  • <b>
  • <big>
  • <blockquote>
  • <br>
  • <cite>
  • <dfn>
  • <div align="...">
  • <em>
  • <font size="..." color="..." face="...">
  • <h1>
  • <h2>
  • <h3>
  • <h4>
  • <h5>
  • <h6>
  • <i>
  • <img src="...">
  • <p>
  • <small>
  • <strike>
  • <strong>
  • <sub>
  • <sup>
  • <tt>
  • <u>
除了以上提到的标签之外,还可以实现TagHandler这个接口来自定义一些标签。具体方法可以看这个文章,这里就不拓展了
 
下面就来用代码演示一下,如何使用TextView支持HTML这个功能
TextView textView = (TextView) findViewById(R.id.text);
String str = "<font color='blue'>百度一下,你就知道</font>";
textView.setText(Html.fromHtml(str));
用了一个简单的font标签对字体的颜色进行修改




另外,如果需要用到href这个链接类型的标签时候,
一定要设置setMovementMethod(LinkMovementMethod.getInstance())这个方法,不然没有效果
而且不能设置android:autoLink属性

img这个图片标签的话要用到反射机制,如下。
String str = "图片:<img src = 'ic_launcher'/><br>";
textView.setText((Html.fromHtml(str, new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
Drawable draw = null;
try {
Field field = R.drawable.class.getField(source);
int resourceId = Integer.parseInt(field.get(null).toString());
draw = getResources().getDrawable(resourceId);
draw.setBounds(0, 0, draw.getIntrinsicWidth(), draw.getIntrinsicHeight());
} catch (Exception e) {
e.printStackTrace();
}
return draw;
}
}, null)));



关于首字母大写
TextView中有一个android:capitalize的属性可以控制字母的大写
这个属性有四个值
  • none(默认)  无操作    
  • sentence  每个句子首字母大写    
  • words   每个单词首字母大写
  • characters  每个字母都大写


关于字的排版
  • android:textScaleX        设置调节字间距,默认为1.0f,float类型的值
  • android:letterSpacing    设置字间距  float类型的值
  • android:lineSpacingExtra        设置行间距
  • android:lineSpacingMultiplier        设置行间距倍数
  • android:singleLine      设置单行
  • android:maxLines       设置 最多多少行
  • android:ems        设置为多少个字符的长度
  • android:width        设置以pixels为单位的长度
  • android:ellipsize      当文本太长时选择的显示方式 start(开头省略号) end(末尾省略号)  middle(中间省略号) marquee(跑马灯),有个                         setMarqueeRepeatLimit的方法可以限制次数

TextView实现选择器点击效果

先在res目录下建一个color目录用于存放颜色选择器


selelctor_text_view_xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@android:color/holo_orange_dark" android:state_pressed="false"></item>
<item android:color="@android:color/holo_green_light" android:state_pressed="true"></item>
</selector>

然后给TextView设置下面两个属性
android:clickable="true"
android:textColor="@color/selector_text_view"

好了。。就是这么简单


关于首字母大写
TextView中有一个android:capitalize的属性可以控制字母的大写
这个属性有四个值
  • none(默认)  无操作    
  • sentence  每个句子首字母大写    
  • words   每个单词首字母大写
  • characters  每个字母都大写
0 0
原创粉丝点击