Android開發(16)-TextView顯示錶情影象和文字
阿新 • • 發佈:2019-02-08
從這個案例中我們可以學到當我們美化圖片美化介面的時候可以在某一區域輸入圖片和文字混搭資訊,第三張圖片按比例縮小,第四張影象有超連結
佈局檔案
[html] view plaincopyprint?- <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">
- <TextView
- android:id
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_margin="10dp"
- android:background="#FFF"/>
- </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
- package com.example.textview3;
- 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.view.Menu;
- import android.widget.TextView;
- publicclass MainActivity extends Activity {
- publicint getResourceId(String name) {
- try {
- // 根據資源的ID的變數名獲得Field的物件,使用反射機制來實現的
- Field field = R.drawable.class.getField(name);
- // 取得並返回資源的id的欄位(靜態變數)的值,使用反射機制
- return Integer.parseInt(field.get(null).toString());
- } catch (Exception e) {
- // TODO: handle exception
- }
- return0;
- }
- @Override
- protectedvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- TextView textView = (TextView) this.findViewById(R.id.textview);
- textView.setTextColor(Color.BLACK);
- textView.setBackgroundColor(Color.WHITE);
- textView.setTextSize(20);// 設定字型的大小
- String html = "影象1<img src='image1'/>影象2<img src='image2'/>影象3<img src='image3'/><p>";
- html += "影象4<a href='http://www.baidu.com'><img src='image4'></a>影象5<img src='image5'/>";
- CharSequence charSequence = Html.fromHtml(html, new ImageGetter() {
- @Override
- public Drawable getDrawable(String source) {
- // TODO Auto-generated method stub
- // 獲得系統資源的資訊,比如圖片資訊
- Drawable drawable = getResources().getDrawable(
- getResourceId(source));
- // 第三個圖片檔案按照50%的比例進行壓縮
- if (source.equals("image3")) {
- drawable.setBounds(0, 0, drawable.getIntrinsicWidth() / 2,
- drawable.getIntrinsicHeight() / 2);
- } else {
- drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
- drawable.getIntrinsicHeight());
- }
- return drawable;
- }
- }, null);
- textView.setText(charSequence);
- textView.setMovementMethod(LinkMovementMethod.getInstance());
- }
- @Override
- publicboolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- returntrue;
- }
- }