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

Android TextView的使用

2016-03-19 00:00 441 查看
摘要: 在项目中经常遇到的问题,做了一个简略的总结,后期会持续更新。

一.TextView中文本信息设置成不同颜色:

1.html方式:

String str = "<font color='red'>中软</font>"
+ "<font color= 'green'>国际</font>";

TextView tvTitle = (TextView) findViewById(R.id.tvTitle);
tvTitle.setText(Html.fromHtml(str));

2. style方式:

SpannableString styledText = new SpannableString("亲爱的小宝,你好");
styledText.setSpan(new TextAppearanceSpan(this, R.style.textColorBlack6), 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
styledText.setSpan(new TextAppearanceSpan(this, R.style.textColorGreen), 3, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

TextView tvCom = (TextView) findViewById(R.id.tvCom);
tvCom.setText(styledText,TextView.BufferType.SPANNABLE);

<style name="textColorBlack6">
<item name="android:textColor">@color/black6</item>
<item name="android:textStyle">italic</item>
</style>

<style name="textColorGreen">
<item name="android:textColor">@color/green</item>
</style>

<style name="textColorBlack">
<item name="android:textColor">@color/black</item>
</style>

<style name="textColorRed">
<item name="android:textColor">@color/buyRed</item>
</style>

特殊处理:

//创建一个 SpannableString对象
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轴方向放大为默认字体的两倍,而高度不变
//SpannableString对象设置给TextView
myTextView.setText(msp);
//设置TextView可点击
myTextView.setMovementMethod(LinkMovementMethod.getInstance());

在使用SpannableString对象时要注意:

Spanned.SPAN_EXCLUSIVE_EXCLUSIVE等的作用:

用来标识在 Span 范围内的文本前后输入新的字符时是否把它们也应用这个效果。分别有

Spanned.SPAN_EXCLUSIVE_EXCLUSIVE(前后都不包括)、

Spanned.SPAN_INCLUSIVE_EXCLUSIVE(前面包括,后面不包括)、

Spanned.SPAN_EXCLUSIVE_INCLUSIVE(前面不包括,后面包括)、

Spanned.SPAN_INCLUSIVE_INCLUSIVE(前后都包括)。

二.TextView 翻滚效果实现

Android 翻滚的TextView

三.TextView 跑马灯效果

TextView添加如下属性:

android:singleLine="true"
android:focusable="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="-1"
android:focusableInTouchMode="true"

Activity中设置TextView添加属性:

textView.setSelected(true);


四.TextView 加滚动条

Android TextView 加滚动条

五.代码中设置控件颜色

Android 代码中设置控件背景颜色,以TextView为例

六.TextView或Button不同点击状态处理背景色

以下是配置button中的文字效果:

drawable/button_font.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#FFF" />
<item android:state_focused="true" android:color="#FFF" />
<item android:state_pressed="true" android:color="#FFF" />
<item android:color="#000" />
</selector>

Button还可以实现更复杂的效果,例如渐变

drawable/button_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">//定义当button 处于pressed 状态时的形态。
<shape>
<gradient android:startColor="#8600ff" />
<stroke     android:width="2dp" android:color="#000000" />
<corners android:radius="5dp" />
<padding android:left="10dp" android:top="10dp"
android:bottom="10dp" android:right="10dp"/>
</shape>
</item>
<item android:state_focused="true">//定义当button获得 focus时的形态
<shape>
<gradient android:startColor="#eac100"/>
<stroke     android:width="2dp" android:color="#333333"    color="#ffffff"/>
<corners android:radius="8dp" />
<padding android:left="10dp" android:top="10dp"
android:bottom="10dp" android:right="10dp"/>
</shape>
</item>
</selector>


最后,需要在包含 button的xml文件里添加两项。假如是 main.xml 文件,

我们需要在<Button />里加两项。

android:focusable="true"
android:backgroud="@drawable/button_color"

这样当你使用Button的时候就可以甩掉系统自带的那黄颜色的背景了,实现个性化的背景,配合应用的整体布局非常之有用啊。

七、TextView实现CheckBox效果选中背景图切换

1.创建一个boolean值isCollect用来保存当前状态,TextView.setSelected(isCollect);

2.设置Selector中Item state_selected="true"时的图片:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/icon_play_active"/>
<item android:state_selected="true" android:drawable="@drawable/icon_play_active"/>
<item android:state_enabled="true" android:drawable="@drawable/icon_play_inactive"/>
</selector>

3.TextView设置背景为Selector,就可以实现了。

八、代码中设置drawableLeft

方法:

public void setCompoundDrawables (Drawable left, Drawable top, Drawable right, Drawable bottom);

相当于xml中设置:

android:drawableLeft="@drawable/icon"

Drawable drawable= getResources().getDrawable(R.drawable.drawable);
/// 这一步必须要做,否则不会显示.
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
myTextview.setCompoundDrawables(drawable,null,null,null);

也或参考另一个函数:

public void setCompoundDrawablesWithIntrinsicBounds (Drawable left, Drawable top, Drawable right, Drawable bottom)

设置drawablePadding:

textView.setCompoundDrawablePadding(int) ;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android_UI TextView