当前位置: 编程技术>移动开发
本页文章导读:
▪经过SpannableString来设置超链接、颜色、字体等属性(转) 通过SpannableString来设置超链接、颜色、字体等属性(转)
转自:http://blog.163.com/hero_213/blog/static/3989121420118165545133/
在Android中,TextView是我们最常用的用来显示文本的控件。
一般情.........
▪ WP7学习之施用字体文件库 WP7学习之使用字体文件库
<TextBlock Text="60" Name="num" FontFamily="DigitalDream.ttf#Digital Dream" FontSize="60" />
很简单的调用,DigitalDream.ttf就是字体文件。下面附件有。效果自己试下哦
......
▪ 自定义绝对格局的实现 自定义绝对布局的实现
android 绝对布局已经过期了为了保证稳定性自定义了一个绝对布局
package cn.kuwo.base.skin.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
impor.........
[1]经过SpannableString来设置超链接、颜色、字体等属性(转)
来源: 互联网 发布时间: 2014-02-18
通过SpannableString来设置超链接、颜色、字体等属性(转)
转自:http://blog.163.com/hero_213/blog/static/3989121420118165545133/
在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>
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" />
<item
android:state_focused="true"
android:color="#ff00ffff" />
<item
android:color="#ff0ff000"/>
</selector>
TextViewLinkActivity.java
package com.snowdream;
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.Color;
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.ForegroundColorSpan;
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轴综合");
//设置字体(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 ,53, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //第一个参数表示项目符号占用的宽度,第二个参数为项目符号的颜色
mTextView.setText(msp);
mTextView.setMovementMethod(LinkMovementMethod.getInstance());
}
}
1 楼
lenomon
2012-03-12
这里有篇实现无下划线的,Android使用TextView实现无下划线超链接
[2] WP7学习之施用字体文件库
来源: 互联网 发布时间: 2014-02-18
WP7学习之使用字体文件库
<TextBlock Text="60" Name="num" FontFamily="DigitalDream.ttf#Digital Dream" FontSize="60" />
很简单的调用,DigitalDream.ttf就是字体文件。下面附件有。效果自己试下哦
[3] 自定义绝对格局的实现
来源: 互联网 发布时间: 2014-02-18
自定义绝对布局的实现
android 绝对布局已经过期了为了保证稳定性自定义了一个绝对布局
package cn.kuwo.base.skin.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
public class MyAbsoluteLayout extends ViewGroup {
private int mPaddingLeft;
private int mPaddingRight;
private int mPaddingTop;
private int mPaddingBottom;
public MyAbsoluteLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyAbsoluteLayout(Context context, AttributeSet attrs) {
super(context, attrs);
mPaddingLeft = attrs
.getAttributeIntValue(android.R.attr.paddingLeft, 0);
mPaddingRight = attrs.getAttributeIntValue(android.R.attr.paddingRight,
0);
mPaddingTop = attrs.getAttributeIntValue(android.R.attr.paddingTop, 0);
mPaddingBottom = attrs.getAttributeIntValue(
android.R.attr.paddingBottom, 0);
}
public MyAbsoluteLayout(Context context) {
super(context);
}
@Override
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
return new MyAbsoluteLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 0, 0);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int count = getChildCount();
int maxHeight = 0;
int maxWidth = 0;
measureChildren(widthMeasureSpec, heightMeasureSpec);
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
if (child.getVisibility() != GONE) {
int childRight;
int childBottom;
MyAbsoluteLayout.LayoutParams lp = (MyAbsoluteLayout.LayoutParams) child
.getLayoutParams();
childRight = lp.x + child.getMeasuredWidth();
childBottom = lp.y + child.getMeasuredHeight();
maxWidth = Math.max(maxWidth, childRight);
maxHeight = Math.max(maxHeight, childBottom);
}
}
maxWidth += mPaddingLeft + mPaddingRight;
maxHeight += mPaddingTop + mPaddingBottom;
maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
setMeasuredDimension(resolveSize(maxWidth, widthMeasureSpec),
resolveSize(maxHeight, heightMeasureSpec));
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
if (child.getVisibility() != GONE) {
MyAbsoluteLayout.LayoutParams lp = (MyAbsoluteLayout.LayoutParams) child
.getLayoutParams();
int childLeft = mPaddingLeft + lp.x;
int childTop = mPaddingTop + lp.y;
child.layout(childLeft, childTop, childLeft
+ child.getMeasuredWidth(), childTop
+ child.getMeasuredHeight());
}
}
}
@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
return p instanceof MyAbsoluteLayout.LayoutParams;
}
@Override
protected ViewGroup.LayoutParams generateLayoutParams(
ViewGroup.LayoutParams p) {
return new MyAbsoluteLayout.LayoutParams(p);
}
public static class LayoutParams extends ViewGroup.LayoutParams {
public int x;
public int y;
public int width;
public int height;
public LayoutParams(int x, int y, int width, int height) {
super(width, height);
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public LayoutParams(Context context, AttributeSet attrset) {
super(context, attrset);
}
public LayoutParams(int width, int height) {
super(width, height);
}
public LayoutParams(ViewGroup.LayoutParams params) {
super(params);
}
}
}
最新技术文章: