TextView之你不知道的使用技巧
2016-12-08 本文已影响947人
code小生
作为程序员,对自己写的代码要尽可能做到优化,但是,往往最简单的却最容易被忽视,比如,今天要给大家说的(只会说平时会被忽略的属性或者技巧,最常用的属性不介绍),TextView的使用奇技淫巧,且听我慢慢道来。
无图无真相(有点丑):
这里写图片描述
下面就是代码(注释很详细)
public class MainActivity extends AppCompatActivity {
private TextView tv1, tv2, tv3, tv4, tv5, tv6, tv8;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
}
private void initView() {
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.tv1);
tv1.setText(getString(R.string.xliff_1, "code小生", "Android爱好者"));
tv2 = (TextView) findViewById(R.id.tv2);
tv2.setTypeface(Typeface.createFromAsset(getAssets(), "simkai.ttf"));
tv3 = (TextView) findViewById(R.id.tv3);
setTextSpannable(tv3);
// 定制超链接
tv4 = (TextView) findViewById(R.id.tv4);
tv4.setText(getClickableSpan(tv4));
//设置该句使设置了链接的文本的超连接起作用
tv4.setMovementMethod(LinkMovementMethod.getInstance());
// 设置多种颜色字体
tv5 = (TextView) findViewById(R.id.tv5);
String str1 = "<font color=\"#404f50\">年轻的我们,</font>";
String str2 = "<font color=\"#ff0000\">即将奔赴战场</font>";
tv5.setText(Html.fromHtml(str1 + str2));
// 加载html中的图片
tv6 = (TextView) findViewById(R.id.tv6);
setHtmlBitmap(tv6);
tv8 = (TextView) findViewById(R.id.tv8);
String text = String.format("¥%1$s 门市价:¥%2$s", "88.8", "150");
int index = text.indexOf("门");
SpannableStringBuilder span = new SpannableStringBuilder(text);
span.setSpan(new ForegroundColorSpan(Color.parseColor("#00ff00")), 0, index - 1, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
span.setSpan(new AbsoluteSizeSpan(28), index, text.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
span.setSpan(new ForegroundColorSpan(Color.parseColor("#afafaf")), index, text.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
tv8.setText(span);
}
//http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0120/2335.html
/**
*
* 设置TextView中的某个词突出显示
*
* @param textView
*/
private void setTextSpannable(TextView textView) {
String text = textView.getText().toString().trim();
int start = text.indexOf('5');
int end = text.length();
Spannable textSpan = new SpannableStringBuilder(text);
textSpan.setSpan(new AbsoluteSizeSpan(30), 0, start, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
// textSpan.setSpan(new AbsoluteSizeSpan(45), start, end - 3, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
// 设置文字颜色
textSpan.setSpan(new ForegroundColorSpan(Color.BLUE), start, end - 3, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textSpan.setSpan(new AbsoluteSizeSpan(30), end - 3, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(textSpan);
}
//给指定文本设置超链接
private SpannableString getClickableSpan(TextView textView) {
String text = textView.getText().toString().trim();
final SpannableString spanStr = new SpannableString(text);
spanStr.setSpan(new UnderlineSpan(), 16, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//设置文字的单击事件
spanStr.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this, "服务条款", Toast.LENGTH_SHORT).show();
}
}, 16, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spanStr.setSpan(new ForegroundColorSpan(Color.BLUE), 16, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// 取消点击时的默认背景色
spanStr.setSpan(new BackgroundColorSpan(Color.WHITE), 16, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spanStr.setSpan(new UnderlineSpan(), 21, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//设置文字的单击事件
spanStr.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this, "隐私政策", Toast.LENGTH_SHORT).show();
}
}, 21, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spanStr.setSpan(new ForegroundColorSpan(Color.GREEN), 21, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spanStr.setSpan(new BackgroundColorSpan(Color.WHITE), 21, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return spanStr;
}
/**
* TextView加载html/xml中的图片和文字
*
* @param textView
*/
private void setHtmlBitmap(TextView textView) {
String imgStr = "<b>this is html text</b><br><img src=\"" + R.mipmap.ic_launcher + "\"/>";
Html.ImageGetter imageGetter = new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
int id = Integer.parseInt(source);
Drawable draw = getResources().getDrawable(id);
draw.setBounds(0, 0, 200, 200);
return draw;
}
};
textView.append(Html.fromHtml(imgStr, imageGetter, null));
}
}
除了代码实现的某些功能外,有一些是在strings.xml中实现的,完整代码已上传至github,关注公众号“code小生”查看地址。