1. 程式人生 > >Android開發(16)-TextView顯示錶情影象和文字

Android開發(16)-TextView顯示錶情影象和文字

從這個案例中我們可以學到當我們美化圖片美化介面的時候可以在某一區域輸入圖片和文字混搭資訊,第三張圖片按比例縮小,第四張影象有超連結

佈局檔案

[html] view plaincopyprint?
  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.     android:paddingBottom="@dimen/activity_vertical_margin"
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"
  7.     android:paddingRight="@dimen/activity_horizontal_margin"
  8.     android:paddingTop="@dimen/activity_vertical_margin"
  9.     tools:context=".MainActivity">
  10.     <TextView
  11.         android:id
    ="@+id/textview"
  12.         android:layout_width="fill_parent"
  13.         android:layout_height="wrap_content"
  14.         android:layout_margin="10dp"
  15.         android:background="#FFF"/>
  16. </RelativeLayout>
<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_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="#FFF" />

</RelativeLayout>


MainActivity.java

[java] view plaincopyprint?
  1. package com.example.textview3; 
  2. import java.lang.reflect.Field; 
  3. import android.os.Bundle; 
  4. import android.app.Activity; 
  5. import android.graphics.Color; 
  6. import android.graphics.drawable.Drawable; 
  7. import android.text.Html; 
  8. import android.text.Html.ImageGetter; 
  9. import android.text.method.LinkMovementMethod; 
  10. import android.view.Menu; 
  11. import android.widget.TextView; 
  12. publicclass MainActivity extends Activity { 
  13.     publicint getResourceId(String name) { 
  14.         try
  15.             // 根據資源的ID的變數名獲得Field的物件,使用反射機制來實現的
  16.             Field field = R.drawable.class.getField(name); 
  17.             // 取得並返回資源的id的欄位(靜態變數)的值,使用反射機制
  18.             return Integer.parseInt(field.get(null).toString()); 
  19.         } catch (Exception e) { 
  20.             // TODO: handle exception
  21.         } 
  22.         return0
  23.     } 
  24.     @Override
  25.     protectedvoid onCreate(Bundle savedInstanceState) { 
  26.         super.onCreate(savedInstanceState); 
  27.         setContentView(R.layout.activity_main); 
  28.         TextView textView = (TextView) this.findViewById(R.id.textview); 
  29.         textView.setTextColor(Color.BLACK); 
  30.         textView.setBackgroundColor(Color.WHITE); 
  31.         textView.setTextSize(20);// 設定字型的大小
  32.         String html = "影象1<img src='image1'/>影象2<img src='image2'/>影象3<img src='image3'/><p>"
  33.         html += "影象4<a href='http://www.baidu.com'><img src='image4'></a>影象5<img src='image5'/>"
  34.         CharSequence charSequence = Html.fromHtml(html, new ImageGetter() { 
  35.             @Override
  36.             public Drawable getDrawable(String source) { 
  37.                 // TODO Auto-generated method stub
  38.                 // 獲得系統資源的資訊,比如圖片資訊
  39.                 Drawable drawable = getResources().getDrawable( 
  40.                         getResourceId(source)); 
  41.                 // 第三個圖片檔案按照50%的比例進行壓縮
  42.                 if (source.equals("image3")) { 
  43.                     drawable.setBounds(0, 0, drawable.getIntrinsicWidth() / 2
  44.                             drawable.getIntrinsicHeight() / 2); 
  45.                 } else
  46.                     drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), 
  47.                             drawable.getIntrinsicHeight()); 
  48.                 } 
  49.                 return drawable; 
  50.             } 
  51.         }, null); 
  52.         textView.setText(charSequence); 
  53.         textView.setMovementMethod(LinkMovementMethod.getInstance()); 
  54.     } 
  55.     @Override
  56.     publicboolean onCreateOptionsMenu(Menu menu) { 
  57.         // Inflate the menu; this adds items to the action bar if it is present.
  58.         getMenuInflater().inflate(R.menu.main, menu); 
  59.         returntrue
  60.     }