android_常用UI控件_01_TextView2
显示图片和文字
MainActivity.java
packagecom.example.android_textview_showqqface;importjava.lang.reflect.Field;importandroid.os.Bundle;importandroid.app.Activity;importandroid.graphics.Color;importandroid.graphics.drawable.Drawable;importandroid.text.Html;importandroid.text.Html.ImageGetter;importandroid.text.method.LinkMovementMethod;importandroid.text.util.Linkify;importandroid.util.Log;importandroid.view.Menu;importandroid.widget.TextView;publicclassMainActivityextendsActivity{privateTextViewtextview01;/***利用反射机制根据资源的id的变量名,获取并返回资源的id*/publicintgetResourceId(Stringname){try{//取得资源的ID的变量名获得field的对象Fieldfield=R.drawable.class.getField(name);//returnInteger.parseInt(field.get(null).toString());returnInteger.parseInt(field.get(null).toString());}catch(Exceptione){//TODO:handleexception}return0;}@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textview01=(TextView)this.findViewById(R.id.textview);Stringhtml="<ahref='http://baidu.com'>DogHome</a><p>";html+="<fontcolor=blue>http://baidu.com</a><p>";html+="<联系电话>tel:15995716443<p><fontcolor='#ff0000'>宠物狗</font><p>dog1<imgsrc='sample_1'/><p>dog2<imgsrc='sample_2'/><p>dog3<imgsrc='sample_3'/>";CharSequencecs;cs=Html.fromHtml(html,newImageGetter(){@OverridepublicDrawablegetDrawable(Stringsource){//TODOAuto-generatedmethodstub//获得系统资源信息,比如图片的信息Drawabledrawable=getResources().getDrawable(getResourceId(source));drawable.setBounds(0,0,drawable.getIntrinsicWidth()/2,drawable.getIntrinsicWidth()/2);returndrawable;}},null);textview01.setText(cs);textview01.setTextColor(Color.BLACK);textview01.setBackgroundColor(Color.GREEN);textview01.setTextSize(20);textview01.setAutoLinkMask(Linkify.ALL);textview01.setMovementMethod(LinkMovementMethod.getInstance());}@OverridepublicbooleanonCreateOptionsMenu(Menumenu){//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.getMenuInflater().inflate(R.menu.main,menu);returntrue;}}
activity_main.xml
<RelativeLayoutxmlns: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"><TextViewandroid:id="@+id/textview"android:layout_margin="10dp"android:layout_width="fill_parent"android:layout_height="wrap_content"android:background="#fff"android:text="@string/hello_world"/></RelativeLayout>
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。