Android入门:TextView
2012-05-19 23:58
246 查看
一、设置部分字体的颜色
通过SpannableStringBuilder style = new SpannableStringBuilder(CharSequence cs);
style.setSpan(new ForegroundColorSpan(color), begin, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(style);
设置,由于此种方法比较复杂,我实现了一个辅助类,能够快速实现部分字体的颜色;
final class TextViewUtil{ public static CharSequence setTextColor(CharSequence str,int begin,int end,int color){ SpannableStringBuilder style = new SpannableStringBuilder(str); style.setSpan(new ForegroundColorSpan(color), begin, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); return style; } }
测试代码:
package org.xiazdong; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.text.Spannable; import android.text.SpannableStringBuilder; import android.text.style.ForegroundColorSpan; import android.widget.TextView; public class MainActivity extends Activity { private TextView tv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView)findViewById(R.id.tv); String str = "hello,xiazdong"; CharSequence cq = TextViewUtil.setTextColor(str, 0, 2, Color.RED); cq = TextViewUtil.setTextColor(cq, 2, 5, Color.GREEN); tv.setText(cq); } }
实现效果:
二、实现跑马灯效果(滚动字幕)
在main.xml布局文件的<TextView>中设置如下:android:singleLine="true" android:focusable="true" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" android:focusableInTouchMode="true"
实现效果:
三、实现超链
在<TextView>中设置如下:android:autolink="all",则如果TextView中字符串为“hello,xiazdong's blog:http://blog.csdn.net/xiazdong, phone:12345678901”
则会自动识别 http://blog.csdn.net/xiazdong和12345678901;
相关文章推荐
- Android基础入门教程——2.4.11 AutoCompleteTextView(自动完成文本框)的基本使用
- Android入门逆引手册 - 09 文本框(TextView)的使用
- Android入门教程四十四之 AutoCompleteTextView(自动完成文本框)的基本使用
- Android零基础入门第17节:Android开发第一个控件,TextView属性和方法大全
- Android入门(3)——使用TextView实现跑马灯效果
- Android入门教程二十三之TextView(文本框)详解
- Android入门笔记 - 界面开发 - RadioButton,CheckBox,Spinner,AutoCompleteTextView,DatePicker,TimePicker
- Android零基础入门第17节:Android开发第一个控件,TextView属性和方法大全
- android入门(一)---UI组件之文本框(TextView)
- Android入门:向TextView添加滚动条(添加<ScrollView>标签)
- Android零基础入门第17节:Android开发第一个控件,TextView属性和方法大全
- Android入门/TextView控件(四)
- Android入门笔记 - 界面开发 - TextView,Button,EditText,Toast
- Android入门(4)——AutoComplete TextView实现动态自动匹配输入的内容
- Android入门(三):使用TextView、EditText 和Button接口组件
- Android零基础入门第17节:TextView属性和方法大全
- Android入门之TextView(纯java)
- Android入门:TextView
- Android开发笔记之自定义view篇(入门一简单的TextView)
- Android入门之AutoCompleteTextView(纯java)