在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" tools:context=".MainActivity" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" /> </RelativeLayout>
java代码实现:
package com.wangfubin.textviewshowimage; import android.annotation.TargetApi; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Build; import android.os.Bundle; import android.text.Spannable; import android.text.SpannableString; import android.text.style.ImageSpan; import android.widget.TextView; @TargetApi(Build.VERSION_CODES.DONUT) public class MainActivity extends Activity { private TextView text; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text = (TextView)findViewById(R.id.text); setimage1(); } /** * * @Title: setimage1 * @Description: TODO(使用ImageSpan对象在TextView组件中显示图片) * @param 设定文件 * @return void 返回类型 * @throws */ private void setimage1() { Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); ImageSpan imageSpan = new ImageSpan(this,bitmap); SpannableString spannableString = new SpannableString("icon"); spannableString.setSpan(imageSpan, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); text.setText(spannableString); } }