下面就是代码(注释很详细)
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)); 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); } /** * * 设置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 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)); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
0 0