TextView常用的属性:
设置字体样式
android:textStyle="italic"
bold:加粗
normal:正常
italic:斜体
代码设置:
//添加删除线
txt1.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
//在代码中设置加粗
txt2.getPaint().setFlags(Paint.FAKE_BOLD_TEXT_FLAG);
//添加下划线–当设置TextView的字体大小时,此方法设置下划线失效
txt3.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
自动识别文本内容,电话,邮箱等并在点击是进行相应跳转
android:autoLink="phone"
文本超过TextView显示范围的展示样式
android:ellipsize="marquee"
当要实现TextView的跑马灯效果:
1、布局中
android:ellipsize="marquee"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
android:marqueeRepeatLimit 设置滚动次数 1代表1次,-1代表无限循环
Java代码设置:
txt1.setSingleLine(true);
txt1.setEllipsize(TruncateAt.MARQUEE);
txt1.setHorizontallyScrolling(true); //让文字可以水平滑动
txt1.setMarqueeRepeatLimit(-1);
2、获取焦点:
1、在xml中添加
android:focusable=“true”
android:focusableInTouchMode=“true”
此方法只能让布局中的一个View获取焦点,也就是说如果有两个及以上TextView要实现跑马灯效果不能通过此方式实现
2、自定义TextView重写方法:
@Override
public boolean isFocused() {
return true;
// return super.isFocused();
}
设置字体
TextView txt9 = (TextView)findViewById(R.id.txt9);
2 Typeface typeface = Typeface.createFromAsset(mContext.getAssets(), "fonts/Jinglei.ttf"); // mContext为上下文
3 tv.setTypeface(typeface );
其他常用属性
android:letterSpacing 文字水平间距
android:letterSpacing的值是一个浮点数,为标准字体的倍数作为间距
android:letterSpacing=“0.1”
android:lineSpacingExtra
这个属性是用来设置文字的行间距的使用dp作单位
android:lineSpacingExtra=“2dp”
SpannableString 和SpannableStringBuilder 设置TextView的显示特效
相关类 | 作用 |
---|---|
ForegroundColorSpan | 为文本设置前景色 |
BackgroundColorSpan | 为文本设置背景色 |
RelativeSizeSpan | 设置文字相对大小 |
StrikethroughSpan | 为文本设置中划线,也就是常说的删除线 |
UnderlineSpan | 为文本设置下划线 |
SuperscriptSpan | 设置上标 |
SubscriptSpan | 设置下标 |
StyleSpan | 为文字设置风格(粗体、斜体) |
ImageSpan | 设置文本图片 |
ClickableSpan | 设置可点击的文本 |
URLSpan | 设置超链接文本 |
Html类
/**
* This class processes HTML strings into displayable styled text.
* Not all HTML tags are supported.
* 该类将HTML字符串处理为可显示的样式文本。并非所有HTML标签都受支持
*/
public class Html {
...
}
txt11.setText(Html.formHtml("<u>下划线</u>、<i>斜体字</i>、<font color='red'>红色字</font>"))