• Android常用UI编程_TextView显示图片和文字(包含超链接)


     1     // 根据资源ID获得Field对象
     2     public int getResourceId(String name) {
     3         try {
     4             // 根据资源的ID的变量名获得Field的对象,使用反射机制来实现的
     5             Field field = R.drawable.class.getField(name);
     6             // 取得并返回资源ID的字段(静态变量)的值,使用反射机制
     7             return Integer.parseInt(field.get(null).toString());
     8         } catch (NoSuchFieldException e) {
     9             e.printStackTrace();
    10         } catch (NumberFormatException e) {
    11             e.printStackTrace();
    12         } catch (IllegalArgumentException e) {
    13             e.printStackTrace();
    14         } catch (IllegalAccessException e) {
    15             e.printStackTrace();
    16         }
    17         return 0;
    18     }
     1     protected void onCreate(Bundle savedInstanceState) {
     2         super.onCreate(savedInstanceState);
     3         setContentView(R.layout.activity_main);
     4 
     5         TextView textview = (TextView) findViewById(R.id.textview);
     6         textview.setTextColor(Color.BLACK);
     7         textview.setBackgroundColor(Color.WHITE);
     8         textview.setTextSize(20);// 设置字体大小
     9         String html = "图像1<img src='image1'/>图像2<img src='image2'/>图像3<img src='image3'/><p>";
    10         html += "图像4<a href='http://www.baidu.com'><img src='image4'></a>图像5<img src='image5'/>";
    11 
    12         CharSequence charSequence = Html.fromHtml(html, new ImageGetter() {
    13 
    14             @Override
    15             public Drawable getDrawable(String source) {
    16                 // 获得系统资源的信息,比如图片信息
    17                 Drawable drawable = getResources().getDrawable(
    18                         getResourceId(source));
    19                 // 处理第三个图片文件安装50%的比例压缩
    20                 if (source.equals("image3")) {
    21                     drawable.setBounds(0, 0, drawable.getIntrinsicHeight() / 2,
    22                             drawable.getIntrinsicWidth() / 2);
    23 
    24                 } else {
    25                     drawable.setBounds(0, 0, drawable.getIntrinsicHeight(),
    26                             drawable.getIntrinsicWidth());
    27                 }
    28 
    29                 return drawable;
    30             }
    31         }, null);
    32 
    33         textview.setText(charSequence);
    34         textview.setMovementMethod(LinkMovementMethod.getInstance());
    35     }
  • 相关阅读:
    Python 内存泄露 内存回收机制
    decimal 格式化
    iis 6 配置PHP
    按照 in (....) 里面的顺序进行排序
    设计模式之 访问者模式
    与数据库的列信息有关
    win32 IFolderView2::GetCurrentFolderFlags的使用
    MySQL防止重复插入相同记录 insert if not exists
    c++扩展Python(未验证)
    c++ 获取桌面图标的坐标与名称
  • 原文地址:https://www.cnblogs.com/humanchan/p/3239487.html
Copyright © 2020-2023  润新知