textview 显示图片和其他
来源:互联网 发布:2016年5月非农数据 编辑:程序博客网 时间:2024/04/30 20:50
一、[Android实例]实现TextView里的文字有不同颜色
转eoe:http://www.eoeandroid.com/thread-4496-1-1.html
import android.text.Html;
TextView t3 = (TextView) findViewById(R.id.text3);
t3.setText(
Html.fromHtml(
"<b>text3:</b> Text with a " +
"<a href=\"http://www.google.com\">link</a> " +
"created in the Java source code using HTML."));
二、TextView显示html文件中的图片
转javaeye:http://da-en.javaeye.com/blog/712415
我们知道要让TextView解析和显示Html代码。可以使用
Spanned text = Html.fromHtml(source);
tv.setText(text);
来实现,这个用起来简单方便。
但是,怎样让TextView也显示Html中<image>节点的图像呢?
我们可以看到fromHtml还有另一个重构:
fromHtml(String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)
实现一下ImageGetter就可以让图片显示了:
ImageGetter imgGetter = new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
Drawable drawable = null;
drawable = Drawable.createFromPath(source); // Or fetch it from the URL
// Important
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable
.getIntrinsicHeight());
return drawable;
}
};
至于TagHandler,我们这里不需要使用,可以直接传null。
参考文档:
http://tech-droid.blogspot.com/2010/06/textview-with-html-content.html英语好的朋友就直接看英文文档吧。
三、Android---文字中插入表情
转载自:http://blog.163.com/spf9190@126/blog/static/50207531201091545954587/
这段时间在做一个短信项目,需要实现短信中插入表情的功能,本一位非常困难,经过一段时间的研究,发现还是比较簡単的,现在总结如下。
以短信输入框为例,短信的输入框是一个EditText,它的append方法不仅可以加入字符串,还可以添加HTML标记。以下就是使用HTML标记添加表情的具体操作。
首先需要构建一个ImageGetter,作用是通过HTML标记获得对应在res目录下的图片:
ImageGetter imageGetter = new ImageGetter() {
@Override
public Drawable getDrawable(String source) {
int id = Integer.parseInt(source);
//根据id从资源文件中获取图片对象
Drawable d = getResources().getDrawable(id);
d.setBounds(0, 0, d.getIntrinsicWidth(),d.getIntrinsicHeight());
return d;
}
};
然后就可以直接往EditText视图中添加
inputLable.append(Html.fromHtml("<img src='"+clickedImageId+"'/>", imageGetter, null));
其中 Html.fromHtml("<img src='"+clickedImageId+"'/>"就是HTML的图片标记,在Android中支持了部分HTML标记的使用(这方面我还在继续研究),HTML标记必须被Html.fromHtml修饰。imageGetter即为之前创建的ImageGetter类型的对象。
很简单的几句代码就解决了问题,不仅在EditText中,在TextView中同样可以这样插入图片。
效果图:
以下代码摘录至android源码里面的MMS项目,其中的
package com.android.mms.ui 里的 MessageListItem.java
package com.android.mms.util 里的 SmileyParser.java
/***
*
* 此方法描述的是: 注意此方法在做表情转换的准备了
* @author:wujun@cqghong.com,ppwuyi@sohu.com
* @version: 2010-5-13 下午03:31:13
*/
private void bindCommonMessage(final MessageItem msgItem) {
if (mDownloadButton != null) {
mDownloadButton.setVisibility(View.GONE);
mDownloadingLabel.setVisibility(View.GONE);
}
// Since the message text should be concatenated with the sender's
// address(or name), I have to display it here instead of
// displaying it by the Presenter.
mBodyTextView.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
// Get and/or lazily set the formatted message from/on the
// MessageItem. Because the MessageItem instances come from a
// cache (currently of size ~50), the hit rate on avoiding the
// expensive formatMessage() call is very high.
CharSequence formattedMessage = msgItem.getCachedFormattedMessage();
if (formattedMessage == null) { //肯定为null应为msgItem.formattedMessage从诞生来就没被注意过一次
formattedMessage = formatMessage(msgItem.mContact, msgItem.mBody, //重点到了
msgItem.mSubject, msgItem.mTimestamp,
msgItem.mHighlight);
msgItem.setCachedFormattedMessage(formattedMessage);
}
mBodyTextView.setText(formattedMessage);
if (msgItem.isSms()) {
hideMmsViewIfNeeded();
} else {
Presenter presenter = PresenterFactory.getPresenter(
"MmsThumbnailPresenter", mContext,
this, msgItem.mSlideshow);
presenter.present();
if (msgItem.mAttachmentType != WorkingMessage.TEXT) {
inflateMmsView();
mMmsView.setVisibility(View.VISIBLE);
setOnClickListener(msgItem);
drawPlaybackButton(msgItem);
} else {
hideMmsViewIfNeeded();
}
}
drawLeftStatusIndicator(msgItem.mBoxId);
drawRightStatusIndicator(msgItem);
}
//------------------------------------------------------------------------------
/***
*
* 此方法描述的是: 开始转换了哦
* @author:wujun@cqghong.com,ppwuyi@sohu.com
* @version: 2010-5-13 下午03:32:52
*/
private CharSequence formatMessage(String contact, String body, String subject,
String timestamp, String highlight) {
CharSequence template = mContext.getResources().getText(R.string.name_colon); //遇到鬼了 <主题:<xliff:g id="SUBJECT">%s</xliff:g>>"
SpannableStringBuilder buf = //把他当作StringBuffer只是它可以放的不是 String 而已他能放跟多类型的东西
new SpannableStringBuilder(TextUtils.replace(template,
new String[] { "%s" },
new CharSequence[] { contact })); //替换成联系人
boolean hasSubject = !TextUtils.isEmpty(subject); //主题
if (hasSubject) {
buf.append(mContext.getResources().getString(R.string.inline_subject, subject)); //buff先在是 联系人 主题 XXXX eg wuyi <主题:dsadasdsa> 我爱我家
}
if (!TextUtils.isEmpty(body)) {
if (hasSubject) {
buf.append(" - "); //如果内容有主题有就+ " - " eg wuyi <主题:sdsadsadsa> -
}
SmileyParser parser = SmileyParser.getInstance(); //获得表情类了哦
buf.append(parser.addSmileySpans(body)); //追查 急切关注中
}
if (!TextUtils.isEmpty(timestamp)) {
buf.append("\n");
int startOffset = buf.length();
// put a one pixel high spacer line between the message and the time stamp as requested
// by the spec.
//把之间的信息和时间戳的要求间隔一个像素的高线
//由规范
buf.append("\n");
buf.setSpan(new AbsoluteSizeSpan(3), startOffset, buf.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
startOffset = buf.length();
buf.append(timestamp);
buf.setSpan(new AbsoluteSizeSpan(12), startOffset, buf.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Make the timestamp text not as dark 改变某区域颜色 时间的地方为特殊颜色
int color = mContext.getResources().getColor(R.color.timestamp_color);
buf.setSpan(new ForegroundColorSpan(color), startOffset, buf.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (highlight != null) {
int highlightLen = highlight.length();
String s = buf.toString().toLowerCase();
int prev = 0;
while (true) {
int index = s.indexOf(highlight, prev);
if (index == -1) {
break;
}
buf.setSpan(new StyleSpan(Typeface.BOLD), index, index + highlightLen, 0);
prev = index + highlightLen;
}
}
return buf;
}
//------------------------------------------------------------
/**
* Adds ImageSpans to a CharSequence that replace textual emoticons such
* as :-) with a graphical version.
*
* @param text A CharSequence possibly containing emoticons
* @return A CharSequence annotated with ImageSpans covering any
* recognized emoticons.
* 添加ImageSpans一个CharSequence的表情符号代替文字等 *如用图形版本:-)。
* 核心是把表情字符替换成ImageSpans的对象
*/
public CharSequence addSmileySpans(CharSequence text) {
SpannableStringBuilder builder = new SpannableStringBuilder(text);
Matcher matcher = mPattern.matcher(text);
while (matcher.find()) {
int resId = mSmileyToRes.get(matcher.group());
//注意下面的一块有点不好理解哦但是是核心
builder.setSpan(new ImageSpan(mContext, resId), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return builder;
}
总结:
android 在将字符转化为表情图像其核心代码为
builder.setSpan(new ImageSpan(mContext, resId), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
原理过程是先匹配到表情字符然后通过new ImageSpan(上下文,表情地址)绘制出一个ImageView然后替换掉表情字符。
Android TextView 支持的HTML标签
<a href="...">
<b>
<big>
<blockquote>
<br>
<cite>
<dfn>
<div align="...">
<em>
<font size="..." color="..." face="...">
<h1>
<h2>
<h3>
<h4>
<h5>
<h6>
<i>
<img src="...">
<p>
<small>
<strike>
<strong>
<sub>
<sup>
<tt>
<u>
要给 TextView 加上效果,方式主要有几种:
第一种,自动应用效果,使用 android:autolink 属性,如:
- <TextView xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/text1"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:autoLink="all"
- android:text="@string/link_text_auto"
- />
- <TextView xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/text1"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:autoLink="all"
- android:text="@string/link_text_auto"
- />
第二种,在文本中使用 <a> 标签,如:
- <string name="link_text_manual"><b>text2:</b> This is some other
- text, with a <a href="http://www.google.com">link</a> specified
- via an <a> tag. Use a \"tel:\" URL
- to <a href="tel:4155551212">dial a phone number</a>
- </string>
- <string name="link_text_manual"><b>text2:</b> This is some other
- text, with a <a href="http://www.google.com">link</a> specified
- via an <a> tag. Use a \"tel:\" URL
- to <a href="tel:4155551212">dial a phone number</a>
- </string>
第三种,和第二种其实是一样的,只不过将文本改在 JAVA 代码中,如:
- TextView t3 = (TextView) findViewById(R.id.text3);
- t3.setText(
- Html.fromHtml(
- "<b>text3:</b> Text with a " +
- "<a href=\"http://www.google.com\">link</a> " +
- "created in the Java source code using HTML."));
- t3.setMovementMethod(LinkMovementMethod.getInstance());
- TextView t3 = (TextView) findViewById(R.id.text3);
- t3.setText(
- Html.fromHtml(
- "<b>text3:</b> Text with a " +
- "<a href=\"http://www.google.com\">link</a> " +
- "created in the Java source code using HTML."));
- t3.setMovementMethod(LinkMovementMethod.getInstance());
第四种,前面三种可以说都是自动的,而第四种就是纯“手工”的了。通过创建 SpanableString 字符串,并在之上创 建一个或多个 Span 来实现丰富的效果。例子如下:
- SpannableString ss = new SpannableString("text4: Click here to dial the phone.");
- ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 6,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- ss.setSpan(new URLSpan("tel:4155551212"), 13, 17,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- TextView t4 = (TextView) findViewById(R.id.text4);
- t4.setText(ss);
- t4.setMovementMethod(LinkMovementMethod.getInstance());
- SpannableString ss = new SpannableString("text4: Click here to dial the phone.");
- ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 6,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- ss.setSpan(new URLSpan("tel:4155551212"), 13, 17,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- TextView t4 = (TextView) findViewById(R.id.text4);
- t4.setText(ss);
- t4.setMovementMethod(LinkMovementMethod.getInstance());
完整的代码见 ApiDemo 吧,下面我提几点需要注意的:
.setMovementMethod,此方法在需要响应用户事件时使用,如点击一个电话号码就跳转到拨号页面。如果不执行这个方法是不会响应事件的,即便文本看着已经是下划线蓝色字了。
.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE,这是在 setSpan 时需要指定的 flag,它的意义我试了很久也没试出来,睡个觉,今天早上才突然有点想法,试之,果然。它是用来标识在 Span 范围内的文本前后输入新的字符时是否把它们也应用这个效果。分别有 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE(前后都不包括)、Spanned.SPAN_INCLUSIVE_EXCLUSIVE(前面包括,后面不包括)、Spanned.SPAN_EXCLUSIVE_INCLUSIVE(前面不包括,后面包括)、Spanned.SPAN_INCLUSIVE_INCLUSIVE(前后都包括)。看个截图就更明白了:
对比一下
以下转自:http://blog.csdn.net/yang_hui1986527/article/details/6776629
在Android中,TextView是我们最常用的用来显示文本的控件。
一般情况下,TextView中的文本都是一个样式。那么如何对于TextView中各个部分的文本来设置字体,大小,颜色,样式,以及超级链接等属性呢?下面我们通过SpannableString的具体实例操作来演示一下。
res-layout-main.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="horizontal">
- <TextView
- android:id="@+id/myTextView"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="horizontal">
- <TextView
- android:id="@+id/myTextView"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
res-color-color.xml
res-color-linkcolor.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:state_pressed="true"
- android:color="#ffffff00"/> <!-- pressed -->
- <item android:state_focused="true"
- android:color="#ff00ffff"/> <!-- focused -->
- <item android:color="#ff0ff000"/> <!-- default -->
- </selector>
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:state_pressed="true"
- android:color="#ffffff00"/> <!-- pressed -->
- <item android:state_focused="true"
- android:color="#ff00ffff"/> <!-- focused -->
- <item android:color="#ff0ff000"/> <!-- default -->
- </selector>
TextViewLinkActivity:
- import java.io.IOException;
- import org.xmlpull.v1.XmlPullParserException;
- import android.app.Activity;
- import android.content.res.ColorStateList;
- import android.content.res.XmlResourceParser;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Color;
- import android.graphics.drawable.Drawable;
- import android.os.Bundle;
- import android.text.SpannableString;
- import android.text.Spanned;
- import android.text.method.LinkMovementMethod;
- import android.text.style.AbsoluteSizeSpan;
- import android.text.style.BackgroundColorSpan;
- import android.text.style.BulletSpan;
- import android.text.style.DrawableMarginSpan;
- import android.text.style.ForegroundColorSpan;
- import android.text.style.IconMarginSpan;
- import android.text.style.ImageSpan;
- import android.text.style.RelativeSizeSpan;
- import android.text.style.ScaleXSpan;
- import android.text.style.StrikethroughSpan;
- import android.text.style.StyleSpan;
- import android.text.style.SubscriptSpan;
- import android.text.style.SuperscriptSpan;
- import android.text.style.TextAppearanceSpan;
- import android.text.style.TypefaceSpan;
- import android.text.style.URLSpan;
- import android.text.style.UnderlineSpan;
- import android.widget.TextView;
- public class TextViewLinkActivity extends Activity {
- TextView mTextView = null;
- SpannableString msp = null;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mTextView = (TextView)findViewById(R.id.myTextView);
- //创建一个 SpannableString对象
- msp = new SpannableString("字体测试字体大小一半两倍前景色背景色正常粗体斜体粗斜体下划线删除线x1x2电话邮件网站短信彩信地图X轴综合/bot");
- //设置字体(default,default-bold,monospace,serif,sans-serif)
- msp.setSpan(new TypefaceSpan("monospace"), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- msp.setSpan(new TypefaceSpan("serif"), 2, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- //设置字体大小(绝对值,单位:像素)
- msp.setSpan(new AbsoluteSizeSpan(20), 4, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- msp.setSpan(new AbsoluteSizeSpan(20,true), 6, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //第二个参数boolean dip,如果为true,表示前面的字体大小单位为dip,否则为像素,同上。
- //设置字体大小(相对值,单位:像素) 参数表示为默认字体大小的多少倍
- msp.setSpan(new RelativeSizeSpan(0.5f), 8, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //0.5f表示默认字体大小的一半
- msp.setSpan(new RelativeSizeSpan(2.0f), 10, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //2.0f表示默认字体大小的两倍
- //设置字体前景色
- msp.setSpan(new ForegroundColorSpan(Color.MAGENTA), 12, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //设置前景色为洋红色
- //设置字体背景色
- msp.setSpan(new BackgroundColorSpan(Color.CYAN), 15, 18, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //设置背景色为青色
- //设置字体样式正常,粗体,斜体,粗斜体
- msp.setSpan(new StyleSpan(android.graphics.Typeface.NORMAL), 18, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //正常
- msp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 20, 22, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //粗体
- msp.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 22, 24, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //斜体
- msp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 24, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //粗斜体
- //设置下划线
- msp.setSpan(new UnderlineSpan(), 27, 30, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- //设置删除线
- msp.setSpan(new StrikethroughSpan(), 30, 33, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- //设置上下标
- msp.setSpan(new SubscriptSpan(), 34, 35, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //下标
- msp.setSpan(new SuperscriptSpan(), 36, 37, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //上标
- //超级链接(需要添加setMovementMethod方法附加响应)
- msp.setSpan(new URLSpan("tel:4155551212"), 37, 39, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //电话
- msp.setSpan(new URLSpan("mailto:webmaster@google.com"), 39, 41, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //邮件
- msp.setSpan(new URLSpan("http://www.baidu.com"), 41, 43, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //网络
- msp.setSpan(new URLSpan("sms:4155551212"), 43, 45, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //短信 使用sms:或者smsto:
- msp.setSpan(new URLSpan("mms:4155551212"), 45, 47, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //彩信 使用mms:或者mmsto:
- msp.setSpan(new URLSpan("geo:38.899533,-77.036476"), 47, 49, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //地图
- //设置字体大小(相对值,单位:像素) 参数表示为默认字体宽度的多少倍
- msp.setSpan(new ScaleXSpan(2.0f), 49, 51, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //2.0f表示默认字体宽度的两倍,即X轴方向放大为默认字体的两倍,而高度不变
- //设置字体(依次包括字体名称,字体大小,字体样式,字体颜色,链接颜色)
- ColorStateList csllink = null;
- ColorStateList csl = null;
- XmlResourceParser xppcolor=getResources().getXml (R.color.color);
- try {
- csl= ColorStateList.createFromXml(getResources(),xppcolor);
- }catch(XmlPullParserException e){
- // TODO: handle exception
- e.printStackTrace();
- }catch(IOException e){
- // TODO: handle exception
- e.printStackTrace();
- }
- XmlResourceParser xpplinkcolor=getResources().getXml(R.color.linkcolor);
- try {
- csllink= ColorStateList.createFromXml(getResources(),xpplinkcolor);
- }catch(XmlPullParserException e){
- // TODO: handle exception
- e.printStackTrace();
- }catch(IOException e){
- // TODO: handle exception
- e.printStackTrace();
- }
- msp.setSpan(new TextAppearanceSpan("monospace",android.graphics.Typeface.BOLD_ITALIC, 30, csl, csllink), 51, 53, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- //设置项目符号
- msp.setSpan(new BulletSpan(android.text.style.BulletSpan.STANDARD_GAP_WIDTH,Color.GREEN), 0 ,msp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //第一个参数表示项目符号占用的宽度,第二个参数为项目符号的颜色
- //设置图片
- Drawable drawable = getResources().getDrawable(R.drawable.icon);
- drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
- msp.setSpan(new ImageSpan(drawable), 53, 57, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- mTextView.setText(msp);
- mTextView.setMovementMethod(LinkMovementMethod.getInstance());
- }
- }
- import java.io.IOException;
- import org.xmlpull.v1.XmlPullParserException;
- import android.app.Activity;
- import android.content.res.ColorStateList;
- import android.content.res.XmlResourceParser;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Color;
- import android.graphics.drawable.Drawable;
- import android.os.Bundle;
- import android.text.SpannableString;
- import android.text.Spanned;
- import android.text.method.LinkMovementMethod;
- import android.text.style.AbsoluteSizeSpan;
- import android.text.style.BackgroundColorSpan;
- import android.text.style.BulletSpan;
- import android.text.style.DrawableMarginSpan;
- import android.text.style.ForegroundColorSpan;
- import android.text.style.IconMarginSpan;
- import android.text.style.ImageSpan;
- import android.text.style.RelativeSizeSpan;
- import android.text.style.ScaleXSpan;
- import android.text.style.StrikethroughSpan;
- import android.text.style.StyleSpan;
- import android.text.style.SubscriptSpan;
- import android.text.style.SuperscriptSpan;
- import android.text.style.TextAppearanceSpan;
- import android.text.style.TypefaceSpan;
- import android.text.style.URLSpan;
- import android.text.style.UnderlineSpan;
- import android.widget.TextView;
- public class TextViewLinkActivity extends Activity {
- TextView mTextView = null;
- SpannableString msp = null;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mTextView = (TextView)findViewById(R.id.myTextView);
- //创建一个 SpannableString对象
- msp = new SpannableString("字体测试字体大小一半两倍前景色背景色正常粗体斜体粗斜体下划线删除线x1x2电话邮件网站短信彩信地图X轴综合/bot");
- //设置字体(default,default-bold,monospace,serif,sans-serif)
- msp.setSpan(new TypefaceSpan("monospace"), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- msp.setSpan(new TypefaceSpan("serif"), 2, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- //设置字体大小(绝对值,单位:像素)
- msp.setSpan(new AbsoluteSizeSpan(20), 4, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- msp.setSpan(new AbsoluteSizeSpan(20,true), 6, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //第二个参数boolean dip,如果为true,表示前面的字体大小单位为dip,否则为像素,同上。
- //设置字体大小(相对值,单位:像素) 参数表示为默认字体大小的多少倍
- msp.setSpan(new RelativeSizeSpan(0.5f), 8, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //0.5f表示默认字体大小的一半
- msp.setSpan(new RelativeSizeSpan(2.0f), 10, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //2.0f表示默认字体大小的两倍
- //设置字体前景色
- msp.setSpan(new ForegroundColorSpan(Color.MAGENTA), 12, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //设置前景色为洋红色
- //设置字体背景色
- msp.setSpan(new BackgroundColorSpan(Color.CYAN), 15, 18, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //设置背景色为青色
- //设置字体样式正常,粗体,斜体,粗斜体
- msp.setSpan(new StyleSpan(android.graphics.Typeface.NORMAL), 18, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //正常
- msp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 20, 22, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //粗体
- msp.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 22, 24, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //斜体
- msp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 24, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //粗斜体
- //设置下划线
- msp.setSpan(new UnderlineSpan(), 27, 30, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- //设置删除线
- msp.setSpan(new StrikethroughSpan(), 30, 33, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- //设置上下标
- msp.setSpan(new SubscriptSpan(), 34, 35, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //下标
- msp.setSpan(new SuperscriptSpan(), 36, 37, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //上标
- //超级链接(需要添加setMovementMethod方法附加响应)
- msp.setSpan(new URLSpan("tel:4155551212"), 37, 39, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //电话
- msp.setSpan(new URLSpan("mailto:webmaster@google.com"), 39, 41, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //邮件
- msp.setSpan(new URLSpan("http://www.baidu.com"), 41, 43, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //网络
- msp.setSpan(new URLSpan("sms:4155551212"), 43, 45, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //短信 使用sms:或者smsto:
- msp.setSpan(new URLSpan("mms:4155551212"), 45, 47, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //彩信 使用mms:或者mmsto:
- msp.setSpan(new URLSpan("geo:38.899533,-77.036476"), 47, 49, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //地图
- //设置字体大小(相对值,单位:像素) 参数表示为默认字体宽度的多少倍
- msp.setSpan(new ScaleXSpan(2.0f), 49, 51, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //2.0f表示默认字体宽度的两倍,即X轴方向放大为默认字体的两倍,而高度不变
- //设置字体(依次包括字体名称,字体大小,字体样式,字体颜色,链接颜色)
- ColorStateList csllink = null;
- ColorStateList csl = null;
- XmlResourceParser xppcolor=getResources().getXml (R.color.color);
- try {
- csl= ColorStateList.createFromXml(getResources(),xppcolor);
- }catch(XmlPullParserException e){
- // TODO: handle exception
- e.printStackTrace();
- }catch(IOException e){
- // TODO: handle exception
- e.printStackTrace();
- }
- XmlResourceParser xpplinkcolor=getResources().getXml(R.color.linkcolor);
- try {
- csllink= ColorStateList.createFromXml(getResources(),xpplinkcolor);
- }catch(XmlPullParserException e){
- // TODO: handle exception
- e.printStackTrace();
- }catch(IOException e){
- // TODO: handle exception
- e.printStackTrace();
- }
- msp.setSpan(new TextAppearanceSpan("monospace",android.graphics.Typeface.BOLD_ITALIC, 30, csl, csllink), 51, 53, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- //设置项目符号
- msp.setSpan(new BulletSpan(android.text.style.BulletSpan.STANDARD_GAP_WIDTH,Color.GREEN), 0 ,msp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //第一个参数表示项目符号占用的宽度,第二个参数为项目符号的颜色
- //设置图片
- Drawable drawable = getResources().getDrawable(R.drawable.icon);
- drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
- msp.setSpan(new ImageSpan(drawable), 53, 57, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- mTextView.setText(msp);
- mTextView.setMovementMethod(LinkMovementMethod.getInstance());
- }
- }
效果预览:
- textview 显示图片和其他
- android TextView和EditText中显示图片
- TextView中显示HTML和图片
- TextView中显示文字和图片
- android TextView和EditText中显示图片
- android TextView和EditText中显示图片
- TextView和EditText中显示图片
- android TextView和EditText中显示图片
- Android TextView中显示HTML和图片
- android textview混合显示图片和文字
- android TextView和EditText中显示图片
- TextView可以显示文字和图片
- TextView显示多彩的颜色和图片
- Android textview字体颜色显示和图片显示
- Android textview字体颜色显示和图片显示
- Android textview字体颜色显示和图片显示
- Android textview字体颜色显示和图片显示
- TextView文本显示图片和文字,竖直滚动显示
- java中的前期绑定和后期绑定
- 一行代码初始化dict
- heap和stack有什么区别
- 跟我一起写 Makefile
- 用sed命令进行正则表达式匹配字符串,如何检验匹配是否成功?
- textview 显示图片和其他
- python图形处理库PIL(Python Image Library)的介绍
- EventBus使用详解(一)——初步使用EventBus
- easyui datagrid如何加载通过ajax获得的json格式的数据
- Android 属性动画(Property Animation) 完全解析 (下)
- EventBus使用详解(二)——EventBus使用进阶
- LeetCode之Remove Duplicates from Sorted Array
- android之ConnectivityManager简介,网络连接状态
- jmeter测试TCP服务器/模拟发送TCP请求