To add a picture to the TextView or edit box EditText, the code is as follows:
Import java.util.regex.matcher;import java.util.regex.pattern;import android.os.bundle;import android.app.Activity;import android.text.Spannable;import android.text.SpannableStringBuilder; import android.text.style.imagespan;import android.widget.edittext;import android.widget.textview;public class mainactivity extends activity{ @Override protected void oncreate (bundle Savedinstancestate) { super.oncreate ( Savedinstancestate); setcontentview (R.layout.activity_main); TextView tv = (TextView) findviewbyid (r.id.tv); string str = "sdfasd★fasljeojg★fad Falsdkjf★asjdkf alsdj★foawpjf sajfadsjf ladjfa ls "; SpannableStringBuilder builder = New spannablestringbuilder (str); string flag = "★"; pattern pattern = Pattern.compile (flag); matcher matcher = Pattern.matcher (str); while (Matcher.find ()) { Builder.setspan (New imagespan (This, r.drawable.ic_launcher), matcher.start (), matcher.end ( ), spannable.span_exclusive_exclusive); &nbSp; } tv.settext (builder); EditText ed = (EditText) findviewbyid (R.ID.EDITTEXT1); ed.settext (builder); }}
The layout file code is as follows:
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns:tools= "Http://schemas.android.com/tools" android:layout_width= "Match_parent" android:layout_height= "Match_parent" android:paddingbottom= "@ Dimen/activity_vertical_margin " android:paddingleft=" @dimen/activity_horizontal_margin " android:paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools:context= ". Mainactivity " > <TextView Android:id= "@+id/tv" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "@string/hello_world" /> <edittext android:id=" @+ Id/edittext1 " android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:layout_alignparentleft= "true" android: layout_below= "@+id/tv" android:layout_margintop= "63DP" android:ems= "Ten" > <requestFocus /> </EditText></RelativeLayout>
:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/48/37/wKiom1QGhO3QXo2-AALHfZggEvU096.jpg "title=" App Treasure screenshot 2014090301.png "alt=" Wkiom1qgho3qxo2-aalhfzggevu096.jpg "/>
This article from the "Sky no traces but I flew" blog, please be sure to keep this source http://glblong.blog.51cto.com/3058613/1548221
Android Note: TextView and edit boxes add pictures, Imagespan,spannablestringbuilder usage