您的位置:首页 > 产品设计 > UI/UE

android_常用UI控件_01_TextView2

2014-05-24 06:41 267 查看
显示图片和文字
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>

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  图片 android import public