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/