@Andro Selva的解釋是正確的,沒有太多的事情要做。無奈的是,事情得到API 24,並且隨後與呼叫
Spanned fromHtml (String source,
int flags,
Html.ImageGetter imageGetter,
Html.TagHandler tagHandler);
列入標誌的更好,我懷疑FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH標誌將降低標準段落終止到的雙「\ n \ n」的單行「\ n」的換行符
鑑於Android版本的歷史〜我無法專門爲Android API 24+編寫軟件!所以...我發現了一個包含兩個額外的自定義標籤的kludge解決方案。
1. <scale factor="x.xx">... </scale>
2. <default>... </default>
都通過這種方法
private void ProcessRelativeSizeTag(float scalefactor, boolean opening, Editable output) {
int len = output.length();
if (opening) {
System.out.println("scalefactor open: " + scalefactor);
output.setSpan(new RelativeSizeSpan(scalefactor), len, len,
Spannable.SPAN_MARK_MARK);
} else {
Object obj = getLast(output, RelativeSizeSpan.class);
int where = output.getSpanStart(obj);
scalefactor = ((RelativeSizeSpan)obj).getSizeChange();
output.removeSpan(obj);
System.out.println("scalefactor close: " + scalefactor);
if (where != len) {
output.setSpan(new RelativeSizeSpan(scalefactor), where, len,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
其從供給到Html.fromHtml
方法,即自定義TagHandler稱爲調用RelativeSizeSpan類:
private static class CustomTagHandler implements Html.TagHandler {
private void ProcessRelativeSizeTag(float scalefactor, boolean opening, Editable output) {
int len = output.length();
if (opening) {
//mSizeStack.push(scalefactor);
System.out.println("scalefactor open: " + scalefactor);
output.setSpan(new RelativeSizeSpan(scalefactor), len, len,
Spannable.SPAN_MARK_MARK);
} else {
Object obj = getLast(output, RelativeSizeSpan.class);
int where = output.getSpanStart(obj);
scalefactor = ((RelativeSizeSpan)obj).getSizeChange();
output.removeSpan(obj);
//scalefactor = (float)mSizeStack.pop();
System.out.println("scalefactor close: " + scalefactor);
if (where != len) {
output.setSpan(new RelativeSizeSpan(scalefactor), where, len,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
...
final HashMap<String, String> mAttributes = new HashMap<>();
@Override
public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
String Attr;
processAttributes(xmlReader);
if ("default".equalsIgnoreCase(tag)) {
ProcessRelativeSizeTag(mDefaultTextSize, opening, output);
return;
}
if ("scale".equalsIgnoreCase(tag)) {
Attr = mAttributes.get("factor");
if (Attr != null && !Attr.isEmpty()) {
float factor = parseFloat(Attr);
if (factor > 0)
ProcessRelativeSizeTag(factor, opening, output);
}
return;
...
}
}
要使用,我設置將Textview
對象的文本大小設置爲1.即1像素!然後在變量mDefaultTextSize
中設置所需的真實文本大小。我有所有這些TextView
延伸爲htmlTextView
中HTML功能:
public class htmlTextView extends AppCompatTextView {
static Typeface mLogo;
static Typeface mGAMZ;
static Typeface mBrush;
static Typeface mStandard;
int GS_PAINTFLAGS = FILTER_BITMAP_FLAG | ANTI_ALIAS_FLAG | SUBPIXEL_TEXT_FLAG | HINTING_ON;
static float mDefaultTextSize;
static Typeface mDefaultTypeface;
etc
}
其中包括公共方法
public void setDefaultTextMetrics(String face, float defaultTextSize) {
mDefaultTypeface = mStandard;
if (face != null) {
if ("gamz".equalsIgnoreCase(face)) {
mDefaultTypeface = mGAMZ;
} else {
if ("brush".equalsIgnoreCase(face)) {
mDefaultTypeface = mBrush;
}
}
}
setTypeface(mDefaultTypeface);
setTextSize(1);
mDefaultTextSize = defaultTextSize;
}
簡單((htmlTextView)tv).setDefaultTextMetrics(null, 30);
調用將我htmlTextView
用我的標準字體作爲默認文本大小爲30.
然後當我g香港專業教育學院是這個例子中fromHtml使用方法:
<string name="htmlqwert">
<![CDATA[
<p><default><scale factor="1.5"><box> qwertQWERT </box></scale></default></p>
<p><default><scale factor="1.5"><box> qwertQWERT </box></scale></default></p>
<p><default><scale factor="1.5"><box> qwertQWERT </box></scale></default></p>
<p><default><scale factor="1.5"><box> qwertQWERT </box></scale></default></p>
]]>
</string>
我的自定義標籤<box>
只是讓我突出顯示文本的背景。看到附加的圖片,使用<default>
標籤與TextView
文字大小設定爲1和<default>
標籤通過30倍調用RelevantSizeSpan
示出一個結果,和一個用:不使用<default>
標籤但設置
<string name="htmlqwert">
<![CDATA[
<p><scale factor="1.5"><box> qwertQWERT </box></scale></p>
<p><scale factor="1.5"><box>qwertQWERT</box></scale></p>
<p><scale factor="1.5"><box>qwertQWERT</box></scale></p>
<p><scale factor="1.5"><box>qwertQWERT</box></scale></p>
]]>
</string>
TextView
文字大小改爲30。在第一種情況下,額外的新線仍然存在,但它只有1個像素高!
NB <scale factor="1.5">...</scale>
標籤沒有實際意義。他們只剩下來自其他測試的文物。
結果:下面這兩個例子有段落間2個換行符但是,在左側的一個,這些線中的一條是隻有1個像素高。我將它留給讀者找出如何將其降低到零,但不要使用0
相關的任何差異截屏的文本大小會有所幫助我猜 – 2012-08-09 11:29:05
添加截圖! – 2012-08-09 11:39:55