wine android字体,Android TextView使用HTML处理字体样式、显示图片

学Android的时候突然想到一个问题:怎么用TextView控件显示带有格式的文字,可否使用Html布局?查了下Android 帮助文档,其提供了android.text.Html类和Html.ImageGetter、Html.TagHandler接口。

其实本不打算写这篇博文的,但看到网络上关于此的文章,基本是:你抄我,我抄你,大家抄来抄去,有用的也就那么一两篇文章,而且说得不明不白,网络就是如此,盗版也成为了一种文化,这就是所谓的拿来主义吧。当然不否认大牛的辛勤劳作,写出的高质量文章;其次是学以致用,个人习惯–总结一下。

先看截图:

373dfc0b37004d7a59d5f6e115888765.png

我们平常使用TextView的setText()方法传递String参数的时候,其实是调用的public final void setText (CharSequence text)方法:

Java

/**

* Sets the string value of the TextView. TextView does not accept

* HTML-like formatting, which you can do with text strings in XML resource files.

* To style your strings, attach android.text.style.* objects to a

* {@link android.text.SpannableString SpannableString}, or see the

*

* Available Resource Types documentation for an example of setting

* formatted text in the XML resource file.

*

* @attr ref android.R.styleable#TextView_text

*/

@android.view.RemotableViewMethod

public final void setText(CharSequence text) {

setText(text, mBufferType);

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15/**

* Sets the string value of the TextView. TextView does not accept

* HTML-like formatting, which you can do with text strings in XML resource files.

* To style your strings, attach android.text.style.* objects to a

* {@link android.text.SpannableString SpannableString}, or see the

*

* Available Resource Types documentation for an example of setting

* formatted text in the XML resource file.

*

* @attr ref android.R.styleable#TextView_text

*/

@android.view.RemotableViewMethod

publicfinalvoidsetText(CharSequencetext){

setText(text,mBufferType);

}

而String类是CharSequence的子类,在CharSequence子类中有一个接口Spanned,即类似html的带标记的文本,我们可以用它来在TextView中显示html。但在上面Android源码注释中有提及TextView does not accept HTML-like formatting。

android.text.Html类共提供了三个方法,可以到Android帮助文档查看。

Java

public static Spanned fromHtml (String source)

public static Spanned fromHtml (String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)

public static String toHtml (Spanned text)

1

2

3

4

5publicstaticSpannedfromHtml(Stringsource)

publicstaticSpannedfromHtml(Stringsource,Html.ImageGetterimageGetter,Html.TagHandlertagHandler)

publicstaticStringtoHtml(Spannedtext)

通过使用第一个方法,可以将Html显示在TextView中:

Java

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

TextView tv=(TextView)findViewById(R.id.textView1);

String html="

TextView使用HTML

强调

斜体

"

+"

超链接HTML入门学习HTML!

颜色1"

+"

颜色2

标题1

标题2

标题3

大于>小于<

" +

"下面是网络图片

2_zhang957411207.jpg%5C%22";

tv.setMovementMethod(ScrollingMovementMethod.getInstance());//滚动

tv.setText(Html.fromHtml(html));

}

1

2

3

4

5

6

7

8

9

10

11

12

13publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

TextViewtv=(TextView)findViewById(R.id.textView1);

Stringhtml="

TextView使用HTML

强调

斜体

"

+"

超链接HTML入门学习HTML!

颜色1"

+"

颜色2

标题1

标题2

标题3

大于>小于<

"+

"下面是网络图片

2_zhang957411207.jpg%5C%22";

tv.setMovementMethod(ScrollingMovementMethod.getInstance());//滚动

tv.setText(Html.fromHtml(html));

}

效果:

3ca83665ce8918b215c848494b81552b.png

可以看出,字体效果是显示出来了,但是图片却没有显示。要实现图片的显示需要使用Html.fromHtml的另外一个重构方法:public static Spanned fromHtml (String source, Html.ImageGetterimageGetter, Html.TagHandler tagHandler)其中Html.ImageGetter是一个接口,我们要实现此接口,在它的getDrawable(String source)方法中返回图片的Drawable对象才可以。

修改后的代码:

Java

ImageGetter imgGetter = new Html.ImageGetter() {

public Drawable getDrawable(String source) {

Drawable drawable = null;

URL url;

try {

url = new URL(source);

drawable = Drawable.createFromStream(url.openStream(), ""); //获取网路图片

} catch (Exception e) {

return null;

}

drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable

.getIntrinsicHeight());

return drawable;

}

};

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15ImageGetterimgGetter=newHtml.ImageGetter(){

publicDrawablegetDrawable(Stringsource){

Drawabledrawable=null;

URLurl;

try{

url=newURL(source);

drawable=Drawable.createFromStream(url.openStream(),"");//获取网路图片

}catch(Exceptione){

returnnull;

}

drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable

.getIntrinsicHeight());

returndrawable;

}

};

这里主要是实现了Html.ImageGetter接口,通过图片的URL地址获取相应的Drawable实例。

不要忘了在Mainifest文件中加入网络访问的权限:

Java

1

友情提示:通过网络获取图片是一个耗时的操作,最好不要放在主线程中,否则容易引起阻塞。

上面介绍的是显示网络上的图片,但如何显示本地的图片呢:

Java

ImageGetter imgGetter = new Html.ImageGetter() {

public Drawable getDrawable(String source) {

Drawable drawable = null;

drawable = Drawable.createFromPath(source); //显示本地图片

drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable

.getIntrinsicHeight());

return drawable;

}

};

1

2

3

4

5

6

7

8

9

10ImageGetterimgGetter=newHtml.ImageGetter(){

publicDrawablegetDrawable(Stringsource){

Drawabledrawable=null;

drawable=Drawable.createFromPath(source);//显示本地图片

drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable

.getIntrinsicHeight());

returndrawable;

}

};

只需将source改为本地图片的路径便可,在这里我使用的是:

Java

String source;

source=getFilesDir()+"/ic_launcher.png";

1

2Stringsource;

source=getFilesDir()+"/ic_launcher.png";

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值