常用屬性
- id
該控件唯一表示 - layout_width
該控件的寬度 - layout_height
該控件的高度 - gravity
該控件內(nèi)部文字的對齊方式 - text
顯示的文本內(nèi)容 - textColor
顯示的文本顏色 - textStyle
顯示的文本樣式澄峰,三個可選值:normal(無效果)何吝,bold(加粗),italic(斜體) - textSize
顯示的文本字體大小 - background
該控件的背景蝴簇,可以是圖片岸蜗、顏色 -
android:shadowColor
設(shè)置陰影顏色,需要與shadowRadius
一起使用拗小,否則沒有效果 -
android:shadowRadius
設(shè)置陰影的模糊程度,需要與shadowColor
一起使用银还,否則沒有效果 -
android:shadowDx
設(shè)置陰影在水平方向的偏移徒仓,默認(rèn)為 0 -
android:shadowDy
設(shè)置陰影在豎直方向的偏移腐碱,默認(rèn)為 0
使用技巧
1、設(shè)置邊框掉弛、背景色症见、圓角、間距等樣式
<?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" />
<!-- 設(shè)置四個圓角的半徑 -->
<corners
android:bottomLeftRadius="10px"
android:bottomRightRadius="10px"
android:topLeftRadius="10px"
android:topRightRadius="10px" />
<!-- 設(shè)置一下邊距,讓空間大一點 -->
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
<!-- 這個是設(shè)置漸變色的,可選屬性有: startColor:起始顏色殃饿;endColor:結(jié)束顏色谋作;centerColor:中間顏色;angle:方向角度,等于0時,從左到右,然后逆時針方向轉(zhuǎn),當(dāng)angle = 90度時從下往上乎芳;type:設(shè)置漸變的類型 -->
<gradient
angle="0"
startColor="#000000"
endColor="#ffffff"
centerColor="#777777" />
</shape>
2遵蚜、設(shè)置四周的圖片
設(shè)置TextView四周的圖片帖池,使用如下屬性:
drawableTop(上)
drawableButtom(下)
drawableLeft(左)
drawableRight(右)
另外,你也可以使用 drawablePadding 來設(shè)置圖片與文字間的 間距
/**
使用 DrawableTop 等方式顯示的圖片在xml里面是不能修改大小的
想要修改需要在Java代碼里面修改
*/
textView = (TextView) findViewById(R.id.tv);
Drawable[] drawable = textView.getCompoundDrawables();
// 數(shù)組下表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 進(jìn)行偏移
注意點:若設(shè)置 top不撑、left 時導(dǎo)致圖片超出 TextView 范圍文兢,則超出部分會隱藏
*/
3、鏈接
1焕檬、使用 android:autoLink="web|email|phone|map"
屬性指定點擊該TextView做出什么處理。
2澳泵、在Java中設(shè)置:
tv.setAutoLinkMask(Linkify.WEB_URLS);
tv.setMovementMethod(LinkMovementMethod.getInstance());
4实愚、字體控制
1、字間距
android:textScaleX:控制字體水平方向的縮放兔辅,默認(rèn)值1.0f
Java使用中setScaleX(2.0f);
2腊敲、行間距
android:lineSpacingExtra:設(shè)置行間距,如"3dp"
android:lineSpacingMultiplier:設(shè)置行間距的倍數(shù)维苔,如"1.2"
5碰辅、對 HTML 的支持
常用支持以下標(biāo)簽
-
<font>
:設(shè)置顏色和字體。 -
<big>
:設(shè)置字體大號 -
<small>
:設(shè)置字體小號 -
<i>
:斜體 -
<b>
:粗體 -
<a>
:網(wǎng)址 -
<img>
:圖片 - 案例1
TextView t1 = (TextView)findViewById(R.id.txtOne);
String s1 = "<font color='blue'><b>百度一下介时,你就知道</b></font><br>";
s1 += "<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));