您的位置:首页 > 移动开发 > Android开发

【Andoid杂谈】Android TextView中显示超文本(HTML)内容

2015-10-20 19:43 633 查看
主要在于html.fromHtml();方法的应用,
以下是代码:


<span style="font-size:18px;"><strong>package yyc.com.htmltextview;

import android.graphics.drawable.Drawable;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import java.net.URL;

import yyc.com.htmltextview.tools.HtmlTextView;

public class MainActivity extends AppCompatActivity {

TextView htmlTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
struct();
init();

}

public static void struct() {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork() // or
// .detectAll()
// for
// all
// detectable
// problems
.penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects() // 探测SQLite数据库操作
.penaltyLog() // 打印logcat
.penaltyDeath().build());
}

private void init(){
htmlTextView=(TextView)findViewById(R.id.html);
String s="<html><head><title>TextView使用HTML</title></head><body><p><strong>强调</strong></p><p><em>斜体</em></p>"
+ "<p><a href=\"http://www.dreamdu.com/xhtml/\">超链接HTML入门</a>学习HTML!</p><p><font color=\"#aabb00\">颜色1"
+ "</p><p><font color=\"#00bbaa\">颜色2</p><p><font color=\"#aabb00\">颜色1"
+ "</p><p><font color=\"#00bbaa\">颜色2</p><p><font color=\"#aabb00\">颜色1"
+ "</p><p><font color=\"#00bbaa\">颜色2</p><p><font color=\"#aabb00\">颜色1"
+ "</p><p><font color=\"#00bbaa\">颜色2</p><p><font color=\"#aabb00\">颜色1"
+ "</p><p><font color=\"#00bbaa\">颜色2</p><h1>标题1</h1><h3>标题2</h3><h6>标题3</h6><p>大于>小于<</p><p>"
+ "下面是网络图片</p><img src=\"http://avatar.csdn.net/0/3/8/2_zhang957411207.jpg\"/></body></html>";
//以下放置内容HTML超文本内容
htmlTextView.setText(Html.fromHtml(s,imgGetter,null));
}

//这里面的resource就是fromhtml函数的第一个参数里面的含有的url
Html.ImageGetter imgGetter = new Html.ImageGetter() {
public Drawable getDrawable(String source) {
Log.i("RG", "source---?>>>" + source);
Drawable drawable = null;
URL url;
try {
url = new URL(source);
Log.i("RG", "url---?>>>" + url);
drawable = Drawable.createFromStream(url.openStream(), ""); // 获取网路图片
} catch (Exception e) {
e.printStackTrace();
return null;
}
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
Log.i("RG", "url---?>>>" + url);
return drawable;
}
};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}</strong></span>


以下是代码:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: