您好,登錄后才能下訂單哦!
顯示圖片和文字
MainActivity.java
package com.example.android_textview_showqqface; import java.lang.reflect.Field; import android.os.Bundle; import android.app.Activity; import android.graphics.Color; import android.graphics.drawable.Drawable; import android.text.Html; import android.text.Html.ImageGetter; import android.text.method.LinkMovementMethod; import android.text.util.Linkify; import android.util.Log; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { private TextView textview01; /** * 利用反射機制根據資源的id的變量名,獲取并返回資源的id */ public int getResourceId(String name){ try { //取得資源的ID的變量名獲得field的對象 Field field = R.drawable.class.getField(name); //return Integer.parseInt(field.get(null).toString()); return Integer.parseInt(field.get(null).toString()); } catch (Exception e) { // TODO: handle exception } return 0; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textview01 = (TextView) this.findViewById(R.id.textview); String html = "<a ; html += "<font color=blue>http://baidu.com</a><p>"; html += "<聯系電話>tel:15995716443<p><font color='#ff0000'>寵物狗</font><p>dog1<img src='sample_1'/><p>dog2<img src='sample_2'/><p>dog3<img src='sample_3'/>"; CharSequence cs ; cs = Html.fromHtml(html, new ImageGetter() { @Override public Drawable getDrawable(String source) { // TODO Auto-generated method stub //獲得系統資源信息,比如圖片的信息 Drawable drawable = getResources().getDrawable(getResourceId(source)); drawable.setBounds(0, 0, drawable.getIntrinsicWidth()/2, drawable.getIntrinsicWidth()/2); return drawable; } }, null); textview01.setText(cs); textview01.setTextColor(Color.BLACK); textview01.setBackgroundColor(Color.GREEN); textview01.setTextSize(20); textview01.setAutoLinkMask(Linkify.ALL); textview01.setMovementMethod(LinkMovementMethod.getInstance()); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
activity_main.xml
<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/textview" android:layout_margin="10dp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#fff" android:text="@string/hello_world" /> </RelativeLayout>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。