Home  >  Article  >  Web Front-end  >  TextView advanced application: TextView displays Html format content and pictures_html/css_WEB-ITnose

TextView advanced application: TextView displays Html format content and pictures_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:46:591330browse

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:id="@ id/htm_tv"
🎜> android:padding="10dp"

android:textSize="16sp";

/>


--- --------------------Realize display of html format text in Activity---------------------- ---------

public class TestActivity extends Activity {

private TextView htmlTv;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_test_html);
/**Get TextView control*/
htmlTv = (TextView) findViewById(R.id. text);
/**We piece together the Html format string ourselves*/
String mHtmlStr = "Html TextView
";
mHtmlStr = "Bold and italic text

";
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));
}
}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn