TextView

本文介绍了在Android应用中如何利用TextView控件显示图片的方法,包括直接设置复合图片、使用ImageSpan以及通过Html.ImageGetter来解析HTML中的图片,提供了详细的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.编译器报  This tag and its children can be replaced by one <TextView/> and a compound drawable

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <ImageView android:id="@+id/image"
     android:layout_gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
  
  <TextView android:id="@+id/text"
   android:layout_gravity="center_vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

在TextView上设置图片

Drawable d=context.getResources().getDrawable(R.drawable.ic_group);
d.setBounds(0,0,72,72);//必须设置,否则不会显示
textView.setCompoundDrawables(d,null,null,null);

 

2. Android系统显示HTML网页的最佳控件为WebView,有时候为了满足特定需求,需要在TextView中显示HTML网页、图片及解析自定义标签。


3. TextView显示插入的图片3种方式:
 (1)、textView.setText(SpannableString);

        在spannableString中指定一段文字被ImageSpan替换,而ImageSpan关联了一个Drawable;

        例:

         ImageSpan imageSpan = new ImageSpan(context, R.drawable.ic_launcher);
         SpannableString spanStr = new SpannableString("http://orgcent.com");
         spanStr.setSpan(imageSpan, spanStr.length()-1, spanStr.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
         textView.setText(spanStr);
 (2)、使用Html.ImageGetter提供网页中要显示的图片

         例:

         richText = Html.fromHtml(htmlImgStr, imageGetter, tagHandler);
         textView.setText(richText);

 (3)、在TextView内部四周可以各设置一张图片

         textView.setCompoundDrawables(leftDrawalbe, topDrawalbe, rightDrawalbe, bottomDrawalbe),

         例:

         mTVText.setText("text1231r41245");
         Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
         drawable.setBounds(0, 0, 50, 50); //必须设置图片大小,否则不显示
         textView.setCompoundDrawables(drawable , null, null, null);

详见

http://orgcent.com/android-textview-imagespan-imagegetter-setcompounddrawables/


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值