当前位置: 移动技术网 > IT编程>移动开发>Android > Android编程开发实现TextView显示表情图像和文字的方法

Android编程开发实现TextView显示表情图像和文字的方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

天变神功,脱帽鞠躬致谢车主,全球巅峰

本文实例讲述了android编程开发实现textview显示表情图像和文字的方法。分享给大家供大家参考,具体如下:

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

布局文件

<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;
public class mainactivity extends activity {
 public int 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
  }
  return 0;
 }
 @override
 protected void 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
 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;
 }
}

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网