常用属性
- id
该控件唯一表示 - layout_width
该控件的宽度 - layout_height
该控件的高度 - gravity
该控件内部文字的对齐方式 - text
显示的文本内容 - textColor
显示的文本颜色 - textStyle
显示的文本样式,三个可选值:normal(无效果),bold(加粗),italic(斜体) - textSize
显示的文本字体大小 - background
该控件的背景,可以是图片、颜色 -
android:shadowColor
设置阴影颜色,需要与shadowRadius
一起使用,否则没有效果 -
android:shadowRadius
设置阴影的模糊程度,需要与shadowColor
一起使用,否则没有效果 -
android:shadowDx
设置阴影在水平方向的偏移,默认为 0 -
android:shadowDy
设置阴影在竖直方向的偏移,默认为 0
使用技巧
1、设置边框、背景色、圆角、间距等样式
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 背景色 -->
<solid android:color="#87CEEB" />
<!-- 边框 -->
<stroke
android:width="2px"
android:color="#000000" />
<!-- 设置四个圆角的半径 -->
<corners
android:bottomLeftRadius="10px"
android:bottomRightRadius="10px"
android:topLeftRadius="10px"
android:topRightRadius="10px" />
<!-- 设置一下边距,让空间大一点 -->
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
<!-- 这个是设置渐变色的,可选属性有: startColor:起始颜色;endColor:结束颜色;centerColor:中间颜色;angle:方向角度,等于0时,从左到右,然后逆时针方向转,当angle = 90度时从下往上;type:设置渐变的类型 -->
<gradient
angle="0"
startColor="#000000"
endColor="#ffffff"
centerColor="#777777" />
</shape>
2、设置四周的图片
设置TextView四周的图片,使用如下属性:
drawableTop(上)
drawableButtom(下)
drawableLeft(左)
drawableRight(右)
另外,你也可以使用 drawablePadding 来设置图片与文字间的 间距
/**
使用 DrawableTop 等方式显示的图片在xml里面是不能修改大小的
想要修改需要在Java代码里面修改
*/
textView = (TextView) findViewById(R.id.tv);
Drawable[] drawable = textView.getCompoundDrawables();
// 数组下表0~3,依次是:左上右下
drawable[1].setBounds(100, 0, 200, 200);
textView.setCompoundDrawables(drawable[0], drawable[1], drawable[2], drawable[3]);
/**
setBounds(int left, int top, int right, int bottom)方法解析(以上方的图片为例):
1、通过 bottom - top 计算出图片的高
2、通过 right - left 计算出图片的宽
3、将该图片置于 TextView 上方边缘水平居中的位置
4、相对该位置,利用 top 作为 marginTop、left 作为 marginLeft 进行偏移
注意点:若设置 top、left 时导致图片超出 TextView 范围,则超出部分会隐藏
*/
3、链接
1、使用 android:autoLink="web|email|phone|map"
属性指定点击该TextView做出什么处理。
2、在Java中设置:
tv.setAutoLinkMask(Linkify.WEB_URLS);
tv.setMovementMethod(LinkMovementMethod.getInstance());
4、字体控制
1、字间距
android:textScaleX:控制字体水平方向的缩放,默认值1.0f
Java使用中setScaleX(2.0f);
2、行间距
android:lineSpacingExtra:设置行间距,如"3dp"
android:lineSpacingMultiplier:设置行间距的倍数,如"1.2"
5、对 HTML 的支持
常用支持以下标签
-
<font>
:设置颜色和字体。 -
<big>
:设置字体大号 -
<small>
:设置字体小号 -
<i>
:斜体 -
<b>
:粗体 -
<a>
:网址 -
<img>
:图片 - 案例1
TextView t1 = (TextView)findViewById(R.id.txtOne);
String s1 = "<font color='blue'><b>百度一下,你就知道</b></font><br>";
s1 += "<a href = 'http://www.baidu.com'>百度</a>";
t1.setText(Html.fromHtml(s1));
t1.setMovementMethod(LinkMovementMethod.getInstance());
- 案例2
// 记得在 drawable 目录下放一张 icon 图片
TextView t1 = (TextView) findViewById(R.id.txtOne);
String s1 = "图片:<img src = 'icon'/><br>";
t1.setText(Html.fromHtml(s1, 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));