TextView
TextView是最简单的一个控件,主要用于在界面上显示一段文本信息。
TextView的一些属性:
gravity:指定文字的对齐方式,可选值有top、bottom、left、right、center等,可以用|来同时指定多个值
textSize:指定文字大小(设置的单位是sp)
textColor:指定文字颜色
drawable:加图片,有drawableBottom、drawableLeft等
padding:离开周围多少dp(是内部压缩),有paddingLeft等
Padding和Margin的区别是:padding是内部压缩,使离开周围多少dp,而Margin是向外
举个例子:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:padding="15dp"
android:text="消息"
android:textSize="15sp"
android:drawableBottom="@mipmap/ic_launcher"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="联系人"
android:textSize="15sp"
android:drawableBottom="@mipmap/ic_launcher"
/>
</LinearLayout>
</RelativeLayout>
效果:
也可以用代码来设置textView的一些属性,比如:
setText:来设置TextView 的文字
getText:得到TextView中的文字
setTextColor:设置文字颜色,
得到自己写的color用的是getResources().getColor(R.color.red),color是个int值
setTextSize:设置文字大小
另外还可以在文字上加下划线等:setPaintFlages(Paint.STRINK_THRU_TEXT_FLAGE)文本的划线
setAutoLinkMask(Linkify.PHONE_NUMBERS);setText(“打电话18700012452”):可以把后面的电话号码设置成一个超链接,点电话号码后可以拨打出去(不过数字长度不要太短,太短的识别不了)
类似的还有:
textView.setAutoLinkMask(Linkify.WEB_URLS);// 当文本内容中包含超链接格式的文本时,自动转换成超链接样式,点击会自动跳转到指定的网页
textView.setAutoLinkMask(Linkify.PHONE_NUMBERS);//自动转手机号码点击它可进入系统拨号界面
textView.setAutoLinkMask(Linkify.EMAIL_ADDRESSES);//自动转邮件地址点击它可发送邮件(要提前设置好自己的电子邮件)
textView.setAutoLinkMask(Linkify.MAP_ADDRESSES);//自动转街道地址点击它可查看位置(前提已安装了google地图)
textView.setAutoLinkMask(Linkify.ALL);//包括上面4种情况
TextView的一些特效介绍:http://www.cnblogs.com/sipher/articles/2576011.html
TextView的跑马灯效果:
ellipsize:设置当文字过长时,该控件该如何显示
有如下值设置:
“start”—–省略号显示在开头
“end”——省略号显示在结尾
“middle”—-省略号显示在中间
“marquee” ——以跑马灯的方式显示(动画横向向左移动)
如果一个页面想实现多个TextView同时跑马灯效果解决方案:给要跑动的textview加上如下代码就行了
textview.setSelected(true);
<TextView
android:id="@+id/paomadeng"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="@string/text"/>
富文本
展示功能比较多的文本,可以支持一部分的HTML,也可以动态的添加一些图片
富文本的介绍:http://www.cnblogs.com/bobodeboke/archive/2013/03/26/2983423.html
android 中textview显示富文本信息具有以下几种方式:
1,利用富文本标签,类似于html标签,如等,不过不能直接作为textview.setText的参数值,而应该静html.fromHtml方法将这些文本转换为charsequence对象。如果想要显示图片的时候,还需要实现imagegetter接口
2,重写ondraw方法
3,利用webview组件显示html页面
4,textview中显示图片还可以使用imagespan对象,该对象用来封装bitmap对象,并通过spannableString对象封装imagespan对象,将其作为settext方法的参数。
用代码实现:
textView = (TextView) findViewById(R.id.testView);
Spanned spanned = Html.fromHtml("这是一个富文本<font color='green'>样式一</font> 加个图像 <img src='ic_launcher'/>在后面",
new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
int id = R.mipmap.ic_launcher;
Class clazz = R.mipmap.class;
try {
Field field = clazz.getDeclaredField(source);
id = field.getInt(clazz);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
Drawable drawable = getResources().getDrawable(id);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
return drawable;
}
}, null);
textView.setText(spanned);
结果: