1、Textview
实现文字说明
<font>设置颜色和字体
<big><small>设置大小号
<i>\<b>斜体、粗体
<a>链接地址
<img>插入图片
Autolink: 自动匹配链接
//点击的时候产生超链接
textView1.setMovementMethod(LinkMovementMethod.getInstance());
下面这幅图中设置了字体的颜色、大小、斜体,上面使用html中的超链接,借用html代码来实现。
而下面的文本是用了autolink系统直接匹配。
<TextView
android:id="@+id/textview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="20sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="all"
android:padding="20sp"
android:text="@string/link_text"
android:id="@+id/textview2"
android:textSize="20sp" />
public class Android_textview1Activity extends Activity
{
/** Called when the activity is first created. */
private TextView textView1,textView2;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView1 = (TextView)this.findViewById(R.id.textview1);
textView2 = (TextView)this.findViewById(R.id.textview2);
String html = "<font color = 'red'>good good study</font><br>";
html+="<font color = '#0000ff'><big><i>day day up</i></big></font><p>";
html+="<big><a href = 'http://www.baidu.com'>百度</a></big>";
CharSequence charSequence = Html.fromHtml(html);
textView1.setText(charSequence);
//点击的时候产生超链接
textView1.setMovementMethod(LinkMovementMethod.getInstance());
String text = "我的URL:http://www.sina.com\n";
text+="my email: 11212@qq.com";
text+="my telephone:132313432";
textView2.setText(text);
textView2.setMovementMethod(LinkMovementMethod.getInstance());
}
}
显示表情图像和文字
html系统类的使用以及Field对像的获取
对头像二进行了缩放,头像三中插入了超链接。
public class Android_textview2Activity extends Activity
{
/** Called when the activity is first created. */
private TextView textView;
public int getResouceId(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
}
return 0;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) this.findViewById(R.id.textview);
textView.setTextColor(Color.BLACK);
textView.setBackgroundColor(Color.WHITE);
textView.setTextSize(20);
String html = "头像1<img src='q1'/>头像2<img src='q2'/>";
html += "<a href='http://blog.csdn.net/shangyu_11'><img src='q3'/></a>头像4<img src='q4'/>";
CharSequence charSequence = Html.fromHtml(html, new ImageGetter()
{
@Override
public Drawable getDrawable(String source)
{
// TODO Auto-generated method stub
// 获得系统资源的信息,比如图片信息
Drawable drawable = getResources().getDrawable(
getResouceId(source));
// 将第二张图片压缩
if (source.equals("q2"))
{