TextView implements hybrid orchestration and textview
TextView
I. Introduction
Here we use the pre-defined Html-like labels in TextView to implement text-and-text hybrid orchestration.
Ii. Method
* 1. Set the html Tag text.
String html = "<font> image 1 </font> ";
Html + = "<font> Image 2 </font> ";
Html + = "<font> Image 3 </font> ";
Html + = "<font> Image 4 </font> ";
Html + = "<font> image 5 </font> ";
* 2. Declare Html. fromHtml for the previous text so that TextView can be parsed as an html Tag.
TV _one.setText (Html. fromHtml (text1 ));
Because there are images, we need to obtain the image source, so the above sentence cannot;
So it is as follows:
CharSequence text = Html. fromHtml (html, new ImageGetter () {center omitted}, null );
New ImageGetter () {omitted in the middle} is complicated. Check the instance code. The essence is to get the ID corresponding to the image in the R file.
* 3. Insert the text of the CharSequence string sequence to the TextView control.
TV _textAndImage.setText (text );
Here is charSequence because the returned value of the Html. fromHtml method isSpanned type,
Take a look at the following class diagram:
Iii. code example
Code
Fry. ActivityDemo2
1 package fry; 2 3 import java. lang. reflect. field; 4 5 import com. example. textViewDemo1.R; 6 7 import android. app. activity; 8 import android. graphics. drawable. drawable; 9 import android. OS. bundle; 10 import android. text. html; 11 import android. text. html. imageGetter; 12 import android. widget. textView; 13 14 public class ActivityDemo2 extends Activity {15 private TextView TV _textAndImage; 16 @ Override17 protected void onCreate (Bundle savedInstanceState) {18 // TODO Auto-generated method stub19 super. onCreate (savedInstanceState); 20 setContentView (R. layout. activity02); 21 setTitle ("TextViewDemo2"); 22 TV _textAndImage = (TextView) findViewById (R. id. TV _textAndImage); 23// Step 1: Set the text24 String html = "<font> image 1 </font> "; 25 html + = "<font> Image 2 </font> "; 26 html + = "<font> Image 3 </font> "; 27 html + = "<font> Image 4 </font> "; 28 html + = "<font> image 5 </font> // Step 2, tell the TextView control that this is html and get the image source in the text30 CharSequence text = Html. fromHtml (html, new ImageGetter () {31 32 public Drawable getDrawable (String source) {33 // TODO Auto-generated method stub34 // obtain the image 35 Based on the image resource ID // getResources is to find the res folder 36 Drawable drawable = getResources () in the project (). getDrawable (getDrawableResurceID (source); 37 // you must add the boundary code. Otherwise, drawable cannot read the picture because the information is incomplete. 38 // left top width height 39 drawable. setBounds (0, 0, drawable. getIntrinsicWidth (), drawable. getIntrinsicHeight (); 40 return drawable; 41} 42}, null); 43// Step 3: insert the text of the CharSequence string sequence into the TextView control.44 TV _textAndImage.setText (text ); 45 46} 47/** 48 * obtain the image resource ID49 * @ param imageName image name 50 * @ return ID51 * 52 */53 private int getDrawableResurceID (String imageName) {54 // use the reflection mechanism to retrieve the image's id55/* 56 * is actually looking for com. example. textViewDemo1.R. drawable. the value of image1, that is, 57 * public static final int image1 = 0x7f020001; 58 * That Is, 0x7f02000159 * For example, image1, returns 0x7f02000160 */61 try {62 Field field = R. drawable. class. getField (imageName); 63 return Integer. parseInt (field. get (null ). toString (); 64} catch (Exception e) {65 // TODO Auto-generated catch block66 e. printStackTrace (); 67} 68 69 return 0; 70 71} 72}
/TextViewDemo1/res/layout/activity02.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 <TextView 7 android:id="@+id/tv_textAndImage" 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content"10 />11 12 </LinearLayout>