{"id":125,"date":"2023-02-18T11:05:20","date_gmt":"2023-02-18T03:05:20","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=125"},"modified":"2023-02-18T11:05:35","modified_gmt":"2023-02-18T03:05:35","slug":"android-spannablestring-detail","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/18\/android-spannablestring-detail\/","title":{"rendered":"Android SpannableString\u5c5e\u6027\u8be6\u89e3"},"content":{"rendered":"<ul>\n<li>1\u3001<code>BackgroundColorSpan<\/code> \u80cc\u666f\u8272 <\/li>\n<li>2\u3001<code>ClickableSpan<\/code> \u6587\u672c\u53ef\u70b9\u51fb\uff0c\u6709\u70b9\u51fb\u4e8b\u4ef6<\/li>\n<li>3\u3001<code>ForegroundColorSpan<\/code> \u6587\u672c\u989c\u8272\uff08\u524d\u666f\u8272\uff09<\/li>\n<li>4\u3001<code>MaskFilterSpan<\/code> \u4fee\u9970\u6548\u679c\uff0c\u5982\u6a21\u7cca(BlurMaskFilter)\u3001\u6d6e\u96d5(EmbossMaskFilter)<\/li>\n<li>5\u3001<code>MetricAffectingSpan<\/code> \u7236\u7c7b\uff0c\u4e00\u822c\u4e0d\u7528<\/li>\n<li>6\u3001<code>RasterizerSpan<\/code> \u5149\u6805\u6548\u679c<\/li>\n<li>7\u3001<code>StrikethroughSpan<\/code> \u5220\u9664\u7ebf\uff08\u4e2d\u5212\u7ebf\uff09<\/li>\n<li>8\u3001<code>SuggestionSpan<\/code> \u76f8\u5f53\u4e8e\u5360\u4f4d\u7b26<\/li>\n<li>9\u3001<code>UnderlineSpan<\/code> \u4e0b\u5212\u7ebf<\/li>\n<li>10\u3001<code>AbsoluteSizeSpan<\/code> \u7edd\u5bf9\u5927\u5c0f\uff08\u6587\u672c\u5b57\u4f53\uff09<\/li>\n<li>11\u3001<code>DynamicDrawableSpan<\/code> \u8bbe\u7f6e\u56fe\u7247\uff0c\u57fa\u4e8e\u6587\u672c\u57fa\u7ebf\u6216\u5e95\u90e8\u5bf9\u9f50\u3002<\/li>\n<li>12\u3001<code>ImageSpan<\/code> \u56fe\u7247<\/li>\n<li>13\u3001<code>RelativeSizeSpan<\/code> \u76f8\u5bf9\u5927\u5c0f\uff08\u6587\u672c\u5b57\u4f53\uff09<\/li>\n<li>14\u3001<code>ReplacementSpan<\/code> \u7236\u7c7b\uff0c\u4e00\u822c\u4e0d\u7528<\/li>\n<li>15\u3001<code>ScaleXSpan<\/code> \u57fa\u4e8ex\u8f74\u7f29\u653e<\/li>\n<li>16\u3001<code>StyleSpan<\/code> \u5b57\u4f53\u6837\u5f0f\uff1a\u7c97\u4f53\u3001\u659c\u4f53\u7b49<\/li>\n<li>17\u3001<code>SubscriptSpan<\/code> \u4e0b\u6807\uff08\u6570\u5b66\u516c\u5f0f\u4f1a\u7528\u5230\uff09<\/li>\n<li>18\u3001<code>SuperscriptSpan<\/code> \u4e0a\u6807\uff08\u6570\u5b66\u516c\u5f0f\u4f1a\u7528\u5230\uff09<\/li>\n<li>19\u3001<code>TextAppearanceSpan<\/code> \u6587\u672c\u5916\u8c8c\uff08\u5305\u62ec\u5b57\u4f53\u3001\u5927\u5c0f\u3001\u6837\u5f0f\u548c\u989c\u8272\uff09<\/li>\n<li>20\u3001<code>TypefaceSpan<\/code> \u6587\u672c\u5b57\u4f53<\/li>\n<li>21\u3001<code>URLSpan<\/code> \u6587\u672c\u8d85\u94fe\u63a5<\/li>\n<\/ul>\n<p><!-- more --><\/p>\n<h3>BackgroundColorSpan \u80cc\u666f\u8272<\/h3>\n<pre><code class=\"language-java\">SpannableString spanText = new SpannableString(&quot;BackgroundColorSpan&quot;);\nspanText.setSpan(new BackgroundColorSpan(Color.GREEN), 0, spanText.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);\nmTextView.append(&quot;\\n&quot;);\nmTextView.append(spanText);<\/code><\/pre>\n<h3>ForegroundColorSpan \u524d\u666f\u8272<\/h3>\n<p>\u6587\u672c\u989c\u8272\uff08\u524d\u666f\u8272\uff09<\/p>\n<pre><code class=\"language-java\">SpannableString spanText = new SpannableString(&quot;ForegroundColorSpan&quot;);\nspanText.setSpan(new ForegroundColorSpan(Color.BLUE), 6, spanText.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);\nmTextView.append(&quot;\\n&quot;);\nmTextView.append(spanText);<\/code><\/pre>\n<h3>MaskFilterSpan \u4fee\u9970\u6548\u679c<\/h3>\n<p>\u4fee\u9970\u6548\u679c\uff0c\u5982\u6a21\u7cca(BlurMaskFilter)\u3001\u6d6e\u96d5(EmbossMaskFilter)<\/p>\n<pre><code class=\"language-java\">SpannableString spanText = new SpannableString(&quot;MaskFilterSpan&quot;);\nint length = spanText.length();\n\/\/\u6a21\u7cca(BlurMaskFilter)\nMaskFilterSpan maskFilterSpan = new MaskFilterSpan(new BlurMaskFilter(3, Blur.OUTER));\nspanText.setSpan(maskFilterSpan, 0, length - 10, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);\n\/\/\u6d6e\u96d5(EmbossMaskFilter)\nmaskFilterSpan = new MaskFilterSpan(new EmbossMaskFilter(new float[]{1,1,3}, 1.5f, 8, 3));\nspanText.setSpan(maskFilterSpan, length - 10, length, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);\nmTextView.append(&quot;\\n&quot;);\nmTextView.append(spanText);<\/code><\/pre>\n<h3>RasterizerSpan \u5149\u6805\u6548\u679c<\/h3>\n<pre><code class=\"language-java\">SpannableString spanText = new SpannableString(&quot;RasterizerSpan&quot;);\nspanText.setSpan(new StrikethroughSpan(), 0, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);\nmTVText.append(&quot;\\n&quot;);\nmTVText.append(spanText);<\/code><\/pre>\n<h3>StrikethroughSpan \u5220\u9664\u7ebf<\/h3>\n<pre><code class=\"language-java\">SpannableString spanText = new SpannableString(&quot;StrikethroughSpan&quot;);\nspanText.setSpan(new StrikethroughSpan(), 0, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);\nmTextView.append(&quot;\\n&quot;);\nmTextView.append(spanText);<\/code><\/pre>\n<h3>SuggestionSpan \u5360\u4f4d\u7b26<\/h3>\n<p>\u76f8\u5f53\u4e8e\u5360\u4f4d\u7b26\uff0c\u4e00\u822c\u7528\u5728EditText\u8f93\u5165\u6846\u4e2d\u3002\u5f53\u53cc\u51fb\u6b64\u6587\u672c\u65f6\uff0c\u4f1a\u5f39\u51fa\u63d0\u793a\u6846\u9009\u62e9\u4e00\u4e9b\u5efa\u8bae\uff08\u63a8\u8350\u7684\uff09\u6587\u5b57\uff0c\u9009\u4e2d\u7684\u6587\u672c\u5c06\u66ff\u6362\u6b64\u5360\u4f4d\u7b26\u3002\u5728\u8f93\u5165\u6cd5\u4e0a\u7528\u7684\u8f83\u591a\u3002<\/p>\n<h3>UnderlineSpan \u4e0b\u5212\u7ebf<\/h3>\n<pre><code class=\"language-java\">SpannableString spanText = new SpannableString(&quot;UnderlineSpan&quot;);\nspanText.setSpan(new UnderlineSpan(), 0, spanText.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);\nmTextView.append(&quot;\\n&quot;);\nmTextView.append(spanText);<\/code><\/pre>\n<h3>AbsoluteSizeSpan \u7edd\u5bf9\u5927\u5c0f\uff08\u6587\u672c\u5b57\u4f53\uff09<\/h3>\n<pre><code class=\"language-java\">SpannableString spanText = new SpannableString(&quot;AbsoluteSizeSpan&quot;);\nspanText.setSpan(new AbsoluteSizeSpan(20, true), 0, spanText.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);\nmTextView.append(&quot;\\n&quot;);\nmTextView.append(spanText);<\/code><\/pre>\n<h3>DynamicDrawableSpan \u8bbe\u7f6e\u56fe\u7247\uff0c\u57fa\u4e8e\u6587\u672c\u57fa\u7ebf\u6216\u5e95\u90e8\u5bf9\u9f50<\/h3>\n<p>\u8bbe\u7f6e\u56fe\u7247\uff0c\u57fa\u4e8e\u6587\u672c\u57fa\u7ebf\u6216\u5e95\u90e8\u5bf9\u9f50\uff0c\u5de6\u8fb9\u56fe\u7247\u57fa\u4e8e\u57fa\u7ebf\u5bf9\u9f50\uff0c\u53f3\u8fb9\u56fe\u7247\u57fa\u4e8e\u5e95\u90e8\u5bf9\u9f50\u3002<\/p>\n<pre><code class=\"language-java\">DynamicDrawableSpan drawableSpan = new DynamicDrawableSpan(DynamicDrawableSpan.ALIGN_BASELINE) {\n    @Override\n    public Drawable getDrawable() {\n        Drawable d = getResources().getDrawable(R.drawable.ic_launcher);\n        d.setBounds(0, 0, 50, 50);\n        return d;\n    }\n};\n\nDynamicDrawableSpan drawableSpan2 = new DynamicDrawableSpan(DynamicDrawableSpan.ALIGN_BOTTOM) {\n    @Override\n    public Drawable getDrawable() {\n        Drawable d = getResources().getDrawable(R.drawable.ic_launcher);\n        d.setBounds(0, 0, 50, 50);\n        return d;\n    }\n};\n\nSpannableString spanText = new SpannableString(&quot;DynamicDrawableSpan&quot;);\nspanText.setSpan(drawableSpan, 3, 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);\nspanText.setSpan(drawableSpan2, 7, 8, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);\nmTVText.append(&quot;\\n&quot;);\nmTVText.append(spanText);<\/code><\/pre>\n<h3>ImageSpan \u56fe\u7247<\/h3>\n<p>\u548cDynamicDrawableSpan\u5dee\u522b\u4e0d\u5927<\/p>\n<pre><code class=\"language-java\">SpannableString spanText = new SpannableString(&quot;ImageSpan&quot;);\nDrawable d = getResources().getDrawable(R.drawable.ic_launcher);\nd.setBounds(0, 0, 50, 50);\nspanText.setSpan(new ImageSpan(d), 3, 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);\nmTextView.append(&quot;\\n&quot;);\nmTextView.append(spanText);<\/code><\/pre>\n<h3>RelativeSizeSpan \u76f8\u5bf9\u5927\u5c0f\uff08\u6587\u672c\u5b57\u4f53\uff09<\/h3>\n<p>\u76f8\u5bf9\u5927\u5c0f\u7684\u6bd4\u4f8b\u662f\u57fa\u4e8e\u5f53\u524d\u6587\u672c\u5b57\u4f53\u5927\u5c0f<\/p>\n<pre><code class=\"language-java\">SpannableString spanText = new SpannableString(&quot;RelativeSizeSpan&quot;);\n\/\/\u53c2\u6570proportion:\u6bd4\u4f8b\u5927\u5c0f\nspanText.setSpan(new RelativeSizeSpan(2.5f), 3, 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);\nmTextView.append(&quot;\\n&quot;);\nmTextView.append(spanText);<\/code><\/pre>\n<h3>ScaleXSpan \u57fa\u4e8ex\u8f74\u7f29\u653e<\/h3>\n<pre><code class=\"language-java\">SpannableString spanText = new SpannableString(&quot;ScaleXSpan&quot;);\n\/\/\u53c2\u6570proportion:\u6bd4\u4f8b\u5927\u5c0f\nspanText.setSpan(new ScaleXSpan(3.8f), 3, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);\nmTextView.append(&quot;\\n&quot;);\nmTextView.append(spanText);<\/code><\/pre>\n<h3>StyleSpan \u5b57\u4f53\u6837\u5f0f<\/h3>\n<p>\u5b57\u4f53\u6837\u5f0f\uff1a\u7c97\u4f53\u3001\u659c\u4f53\u7b49<\/p>\n<pre><code class=\"language-java\">SpannableString spanText = new SpannableString(&quot;StyleSpan&quot;);\n\/\/Typeface.BOLD_ITALIC: \u7c97\u4f53+\u659c\u4f53\nspanText.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 3, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);\nmTextView.append(&quot;\\n&quot;);\nmTextView.append(spanText);<\/code><\/pre>\n<h3>SubscriptSpan \u4e0b\u6807\uff08\u6570\u5b66\u516c\u5f0f\u4f1a\u7528\u5230\uff09<\/h3>\n<pre><code class=\"language-java\">SpannableString spanText = new SpannableString(&quot;SubscriptSpan&quot;);\nspanText.setSpan(new SubscriptSpan(), 6, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);\nmTextView.append(&quot;\\n&quot;);\nmTextView.append(spanText);<\/code><\/pre>\n<h3>SuperscriptSpan \u4e0a\u6807\uff08\u6570\u5b66\u516c\u5f0f\u4f1a\u7528\u5230\uff09<\/h3>\n<pre><code class=\"language-java\">SpannableString spanText = new SpannableString(&quot;SuperscriptSpan&quot;);\nspanText.setSpan(new SuperscriptSpan(), 6, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);\nmTextView.append(&quot;\\n&quot;);\nmTextView.append(spanText);<\/code><\/pre>\n<h3>TextAppearanceSpan \u6587\u672c\u5916\u8c8c<\/h3>\n<p>\u5305\u62ec\u5b57\u4f53\u3001\u5927\u5c0f\u3001\u6837\u5f0f\u548c\u989c\u8272\u3002\u7cfb\u7edf\u8fd8\u63d0\u4f9b\u4e86\u76f8\u5173\u503cTextAppearance_Small, TextAppearance_Large\u7b49\u3002\u5982\u6709\u9700\u8981\u53ef\u5728\u4ee5\u4e0a\u6837\u5f0f\u57fa\u7840\u4e0a\u4fee\u6539\u3002<\/p>\n<pre><code class=\"language-java\">SpannableString spanText = new SpannableString(&quot;TextAppearanceSpan&quot;);\n\/\/\u82e5\u9700\u81ea\u5b9a\u4e49TextAppearance\uff0c\u53ef\u4ee5\u5728\u7cfb\u7edf\u6837\u5f0f\u4e0a\u8fdb\u884c\u4fee\u6539\nspanText.setSpan(new TextAppearanceSpan(this, android.R.style.TextAppearance_Medium), 6, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);\nmTextView.append(&quot;\\n&quot;);\nmTextView.append(spanText);<\/code><\/pre>\n<h3>TypefaceSpan \u6587\u672c\u5b57\u4f53<\/h3>\n<pre><code class=\"language-java\">SpannableString spanText = new SpannableString(&quot;TypefaceSpan&quot;);\n\/\/\u82e5\u9700\u4f7f\u7528\u81ea\u5b9a\u4e49\u5b57\u4f53\uff0c\u53ef\u80fd\u8981\u91cd\u5199\u7c7bTypefaceSpan\nspanText.setSpan(new TypefaceSpan(&quot;monospace&quot;), 3, 10,\nSpannable.SPAN_INCLUSIVE_EXCLUSIVE);\nmTextView.append(&quot;\\n&quot;);\nmTextView.append(spanText);<\/code><\/pre>\n<h3>URLSpan \u6587\u672c\u8d85\u94fe\u63a5<\/h3>\n<pre><code class=\"language-java\">SpannableString spanText = new SpannableString(&quot;URLSpan&quot;);\nspanText.setSpan(new URLSpan(&quot;http:\/\/www.androidios.cc&quot;), 10, spanText.length(),\nSpannable.SPAN_INCLUSIVE_EXCLUSIVE);\nmTextView.append(&quot;\\n&quot;);\nmTextView.append(spanText);\n\/\/\u8ba9URLSpan\u53ef\u4ee5\u70b9\u51fb\nmTextView.setMovementMethod(new LinkMovementMethod());<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001BackgroundColorSpan \u80cc\u666f\u8272 2\u3001ClickableSpan \u6587\u672c\u53ef\u70b9\u51fb\uff0c\u6709\u70b9\u51fb\u4e8b\u4ef6 3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[],"class_list":["post-125","post","type-post","status-publish","format-standard","hentry","category-android-basic"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=125"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/125\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}