Take notes and record something ~
Remember to write an article "Android Development note-Personalized TextView (Sina Weibo)": http://www.cnblogs.com/lichenwei/p/4411607.html
The main way to realize personalized textview in the article is to make some individualized processing of the keywords by means of substitution, and then to add a realization mode at night.
The usual, look first:
The night brings this realization through the Fromhtml method that the Android official gives us, this method can HTML format the string, let some control such as TextView "know" HTML.
Take a look at the official document's description of the HTML class:
This class processes the HTML strings into displayable styled text. Not all HTML tags is supported.
This class allows HTML code to be identified, but does not support all HTML code, but it is sufficient for our daily development.
And look at the Fromhtml method.
Fromhtml has 2 overloaded methods, let me simply say:
1, when you need to convert the HTML code is plain text, such as hyperlinks, line breaks, mail sending and receiving, then directly use the first method, your text information directly into the method.
2, when you need to convert the HTML code is a picture, such as , then you need to use the second method of overloading, this method has a Imagegetter object, the implementation of this class will find it callback an abstract getdrawable method, In this method, we can download the remote image, obtain the local resource image, and so on. The third parameter, Taghandler, is to customize some of the tags that are not part of the HTML code, and generally we don't use them, and we just set them to null.
All right, just talk to the code.
This is the layout file, very simple on a textview
1 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 >6 7 <TextView8 Android:id= "@+id/tv_1"9 Android:layout_width= "Wrap_content"Ten Android:layout_height= "Wrap_content" One /> A - </Relativelayout>
And look at the main code.
1 PackageCom.example.mytestdemo;2 3 ImportJava.lang.reflect.Field;4 5 Importandroid.app.Activity;6 Importandroid.graphics.drawable.Drawable;7 ImportAndroid.os.Bundle;8 Importandroid.text.Html;9 ImportAndroid.text.Html.ImageGetter;Ten ImportAndroid.widget.TextView; One A Public classMainactivityextendsActivity { - - PrivateTextView Mtextview; the - @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); - +Mtextview =(TextView) Findviewbyid (r.id.tv_1); AString html = " Nice hot weather. <p/> Okay, I have air conditioning! "; at -Charsequence charsequence = html.fromhtml (Html,NewImagegetter () { - - @Override - Publicdrawable getdrawable (String source) { - //Gets the R resource ID based on the resource variable name, and then gets the Drawable object based on the resource ID inDrawable drawable =getresources (). Getdrawable (Getresourceid (source)); - //compress the picture (I use the original image here) toDrawable.setbounds (0, 0, Drawable.getintrinsicwidth (), Drawable.getintrinsicheight ()); + returndrawable; - } the},NULL); * //Set Text information $ Mtextview.settext (charsequence);Panax Notoginseng - } the /** + * Returns the file ID of the resource file based on the variable name of the resource file A * @paramname the * @return + */ - Public intGetresourceid (String name) { $ Try { $ //using the Java reflection mechanism to get the R resource ID -Field field = R.drawable.class. GetField (name); - returnInteger.parseint (Field.get (NULL). toString ()); the}Catch(nosuchfieldexception e) { - e.printstacktrace ();Wuyi}Catch(illegalaccessexception e) { the e.printstacktrace (); -}Catch(IllegalArgumentException e) { Wu e.printstacktrace (); - } About return0; $ } - -}
The code here has a Getresourceid method, I used the reflection mechanism of Java, through the variable name of the resource file to get to the R file resource ID, so that we get the Drawable object in the Imagegetter.
Then can use the drawable under the SetBounds method to compress the picture, of course, if the picture processing good, do not compress is also possible.
Implementation is so simple, call it off!
Balla_ Rabbit
Source: http://www.cnblogs.com/lichenwei/
The copyright of this article is owned by the author and the blog Park, welcome reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to give the original link.
is looking at my blog This children's shoes, I see you imposing, there is a faint of the king's Breath, there will be a future! Next to the word "recommended", you can conveniently point it, action quasi, I do not accept a penny, you also good to come back to me!
Android Development Notes-Colorful TextView