android 使用html标签
2015-08-05 14:16
316 查看
Android中使用html标签很大程度上提高了开发的灵活性。下面是一个简单的使用示例,设置了字体颜色并在中间嵌入了一张图片。
效果如下:
import android.app.Activity; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.text.Html; import android.text.Spanned; import android.widget.TextView; /** * Created by hsji on 2015/8/5. */ public class HtmlStringActivity extends Activity{ TextView content; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_html_string); content = (TextView) findViewById(R.id.content); /** * This methos is called when the HTML parser encounters an <img> tag. * The source argument is the string from the "src" attribute; * the return value should be a Drawable representation of the image or null for a generic replacement image. * Make sure you call setBounds() on your Drawable if it doesn't already have its bounds set. */ Html.ImageGetter imageGetter = new Html.ImageGetter(){ @Override public Drawable getDrawable(String source) { Drawable drawable = null; try { drawable = getResources().getDrawable(Integer.parseInt(source)); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); }catch (NumberFormatException e){ e.printStackTrace(); } return drawable; } }; StringBuffer sb = new StringBuffer(); sb.append("<font color=\"#ff0000\">我</font>") .append("<img src=\""+R.drawable.heart+"\"/>") .append("<font color=\"#ff0000\">董英</font>"); Spanned spanned = Html.fromHtml(sb.toString(),imageGetter,null); content.setText(spanned); } }
效果如下:
相关文章推荐
- Android开发之ADT中无Annotation Processin的解决办法
- 简单的Android Sqlite 使用
- Android 悬浮窗的创建与移除
- Android中的Cursor关闭问题
- Android中Service(服务)详解
- android学习三:json的使用
- Android开发之注解式框架ButterKnife在ADT中的设置
- Android Api Demos登顶之路(二十)Screen Orientation
- Android使用Handler实现线程池的效果,实现照片墙应用
- android获取共享文件信息
- android模拟器访问本地ip
- Android studio gradle debug模式用Release签名
- Android 4.0 事件输入(Event Input)系统
- Android性能调优
- Android getevent/sendevent详解
- Android中添加布局和初始化布局总结
- 如何“任性”使用Android的drawText()
- Facebook工程师是如何改进他们Android客户端的
- Android中批处理drawable-xxx目录中图片资源的那些事儿
- Android Wifi锁之WifiLock