我有一個內部具有html的文本視圖。在設備上它被正確渲染,但在預覽中它看起來像純HTML。Android:使用Eclipse圖形佈局工具的TextView中的HTML
是否有可能在圖形佈局工具而不是純HTML中看到最終結果?
在此先感謝!
我有一個內部具有html的文本視圖。在設備上它被正確渲染,但在預覽中它看起來像純HTML。Android:使用Eclipse圖形佈局工具的TextView中的HTML
是否有可能在圖形佈局工具而不是純HTML中看到最終結果?
在此先感謝!
所以我知道很多時間都過去了,但是我發現了一種在Android Studio的佈局編輯器中預覽HTML的方法(儘管不瞭解Eclipse)。
所以我剛剛創建一個自定義的TextView callet HtmlTextView:
<com.example.app.custom.views.HtmlTextView
android:text="@string/your_html_string"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
字符串資源如下所示:
<string name="your_html_string"><![CDATA[My <b><font color=\'#E55720\'>AWESOME</font></b> html]]></string>
public class HtmlTextView extends TextView {
public HtmlTextView(Context context) {
super(context);
}
public HtmlTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public HtmlTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void setText(CharSequence text, BufferType type) {
Spanned html = null;
if(text != null) {
html = Html.fromHtml(text.toString());
super.setText(html, BufferType.SPANNABLE);
} else {
super.setText(text, type);
}
}
}
,它只是一個使用它的事情後
希望它可以幫助別人!
不太清楚,但試試這個
tv.setText(Html.fromHtml(yourString), TextView.BufferType.SPANNABLE);
或嘗試這種拿的WebView而不是TextView的
webview.loadData(yourString,"text/html","utf-8");
AFAIK Android不顯示HTML中的TextView在圖形佈局工具的渲染。它只會顯示您何時運行您的應用程序。
TextView
類已使用Html.fromHtml
支持一些基本的html標記。
我的Eclipse佈局,預覽並未出現罷工標籤,所以我從延長的@Arthur答案:
public class HtmlTextView extends TextView {
CustomHtmlTagHandler tagHandler = new CustomHtmlTagHandler();
public HtmlTextView(Context context) {
super(context);
}
public HtmlTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public HtmlTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void setText(CharSequence text, BufferType type) {
Spanned html = null;
if(text != null) {
html = Html.fromHtml(text.toString(), null, tagHandler);
super.setText(html, BufferType.SPANNABLE);
} else {
super.setText(text, type);
}
}
}
這是CustomHtmlTagHandler:
public class CustomHtmlTagHandler implements TagHandler {
public void handleTag(boolean opening, String tag, Editable output,
XMLReader xmlReader) {
if(tag.equalsIgnoreCase("strike") || tag.equals("s")) {
processStrike(opening, output);
}
}
private void processStrike(boolean opening, Editable output) {
int len = output.length();
if(opening) {
output.setSpan(new StrikethroughSpan(), len, len, Spannable.SPAN_MARK_MARK);
} else {
Object obj = getLast(output, StrikethroughSpan.class);
int where = output.getSpanStart(obj);
output.removeSpan(obj);
if (where != len) {
output.setSpan(new StrikethroughSpan(), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
private Object getLast(Editable text, Class kind) {
Object[] objs = text.getSpans(0, text.length(), kind);
if (objs.length == 0) {
return null;
} else {
for(int i = objs.length;i>0;i--) {
if(text.getSpanFlags(objs[i-1]) == Spannable.SPAN_MARK_MARK) {
return objs[i-1];
}
}
return null;
}
}
}
你做了什麼?請展示 。 – GrIsHu
AFAIK Android不會在圖形佈局工具中的TextView中顯示HTML的呈現。它只會顯示您何時運行您的應用程序。 – GrIsHu
猜猜這就是我期待閱讀的答案:/看起來我每次修改文本時都必須編譯應用程序。 – Arthur