Android开发 TextView显示表情图像和文字

Android开发 TextView显示表情图像和文字

从这个案例中我们可以学到当我们美化图片美化界面的时候可以在某一区域输入图片和文字混搭信息,第三张图片按比例缩小,第四张图像有超链接

布局文件

[html]  view plain  copy
  1. <RelativeLayout xmlns: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.   
  11.     <TextView  
  12.         android:id="@+id/textview"  
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_margin="10dp"  
  16.         android:background="#FFF" />  
  17.   
  18. </RelativeLayout>  


MainActivity.java

[java]  view plain  copy
  1. package com.example.textview3;  
  2. import java.lang.reflect.Field;  
  3.   
  4. import android.os.Bundle;  
  5. import android.app.Activity;  
  6. import android.graphics.Color;  
  7. import android.graphics.drawable.Drawable;  
  8. import android.text.Html;  
  9. import android.text.Html.ImageGetter;  
  10. import android.text.method.LinkMovementMethod;  
  11. import android.view.Menu;  
  12. import android.widget.TextView;  
  13.   
  14. public class MainActivity extends Activity {  
  15.     public int getResourceId(String name) {  
  16.         try {  
  17.             // 根据资源的ID的变量名获得Field的对象,使用反射机制来实现的  
  18.             Field field = R.drawable.class.getField(name);  
  19.             // 取得并返回资源的id的字段(静态变量)的值,使用反射机制  
  20.             return Integer.parseInt(field.get(null).toString());  
  21.         } catch (Exception e) {  
  22.             // TODO: handle exception  
  23.         }  
  24.         return 0;  
  25.     }  
  26.     @Override  
  27.     protected void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.activity_main);  
  30.         TextView textView = (TextView) this.findViewById(R.id.textview);  
  31.         textView.setTextColor(Color.BLACK);  
  32.         textView.setBackgroundColor(Color.WHITE);  
  33.         textView.setTextSize(20);// 设置字体的大小  
  34.         String html = "图像1<img src='image1'/>图像2<img src='image2'/>图像3<img src='image3'/><p>";  
  35.         html += "图像4<a href='http://www.baidu.com'><img src='image4'></a>图像5<img src='image5'/>";  
  36.   
  37.         CharSequence charSequence = Html.fromHtml(html, new ImageGetter() {  
  38.   
  39.             @Override  
  40.             public Drawable getDrawable(String source) {  
  41.                 // TODO Auto-generated method stub  
  42.                 // 获得系统资源的信息,比如图片信息  
  43.                 Drawable drawable = getResources().getDrawable(  
  44.                         getResourceId(source));  
  45.                 // 第三个图片文件按照50%的比例进行压缩  
  46.                 if (source.equals("image3")) {  
  47.                     drawable.setBounds(00, drawable.getIntrinsicWidth() / 2,  
  48.                             drawable.getIntrinsicHeight() / 2);  
  49.                 } else {  
  50.                     drawable.setBounds(00, drawable.getIntrinsicWidth(),  
  51.                             drawable.getIntrinsicHeight());  
  52.                 }  
  53.                 return drawable;  
  54.             }  
  55.         }, null);  
  56.         textView.setText(charSequence);  
  57.         textView.setMovementMethod(LinkMovementMethod.getInstance());  
  58.     }  
  59.       
  60.   
  61.     @Override  
  62.     public boolean onCreateOptionsMenu(Menu menu) {  
  63.         // Inflate the menu; this adds items to the action bar if it is present.  
  64.         getMenuInflater().inflate(R.menu.main, menu);  
  65.         return true;  
  66.     }  
  67.   
  68. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值