android_常用UI控件_01_TextView2

顯示圖片和文字

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 href='http://baidu.com'>Dog Home</a><p>";
        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>

wKioL1N_zzeDoiifAAHRoBS_24c538.jpg

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章