Home > Article > Web Front-end > TextView advanced application: TextView displays Html format content and pictures_html/css_WEB-ITnose
TextView is one of the most commonly used components in our development. The most commonly used function is mainly used for text display. You can set the text size, color and other effects. If you think that the function of TextView is only used to display text, you are totally wrong. It has more uses, such as displaying pictures, realizing some text click events, and realizing URLs, mobile phone numbers with links, etc., all of which can be used in TextView. Once realized. Be strong!
TextView is often used in projects to display text in Html format. I will simply post it today. Take a note.
-------------------------Simple layout main_test_html.xml, just a TextView--------- ---------------------
< ;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation= "vertical" >
🎜> android:padding="10dp"
/>
--- --------------------Realize display of html format text in Activity---------------------- ---------
public class TestActivity extends Activity {
private TextView htmlTv;
";
mHtmlStr = "With link, click to jump directly to Sina, haha";
/* *To use Html.fromHtml, convert the string containing HTML tags into displayable text (CharSequence: the parent class of String, StringBuffer, etc.) style*/
CharSequence charSequence = Html.fromHtml(mHtmlStr);
// Assign value to TextView through setText
htmlTv.setText(charSequence);
/**************What if we want to add pictures to TextView?****************************** *************/
/**To use the Html class ImageGetter interface*/
ImageGetter imageGetter = new ImageGetter() {
@Override
public Drawable getDrawable(String source) {
int id = Integer.parseInt (source);
Drawable drawable = getResources().getDrawable(id);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
return drawable;
}
};
/**You can display pictures through the following methods*/
htmlTv.setText(Html.fromHtml("", imageGetter, null));
}
}