在TextView中显示图片

来源:互联网 发布:张本天杰 数据 编辑:程序博客网 时间:2024/04/28 04:22
<?xml version="1.0" encoding="utf-8"?><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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <TextView        android:id="@+id/tv1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"       /></RelativeLayout>


package com.example.textviewshowimg;import android.os.Bundle;import android.app.Activity;import android.graphics.drawable.Drawable;import android.text.Html;import android.text.Html.ImageGetter;import android.view.Menu;import android.widget.TextView;public class MainActivity extends Activity {private TextView tv1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);setupViews();}private void setupViews() {tv1 = (TextView) findViewById(R.id.tv1);tv1.append(Html.fromHtml("<img src='" + R.drawable.u + "' />",imageGetter, null));}ImageGetter imageGetter = new ImageGetter() {@Overridepublic Drawable getDrawable(String source) {int id = Integer.parseInt(source);Drawable drawable = getResources().getDrawable(id);drawable.setBounds(0, 0, drawable.getIntrinsicWidth()/2,drawable.getIntrinsicHeight()/2);return drawable;}};@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}

效果:


原创粉丝点击