android系列1.TextView学习

1.创建TextView方法.

a,方法直接配置文本中设置

1 <TextView  
2     android:layout_width="fill_parent" 
3     android:layout_height="wrap_content" 
4     android:text="@string/hello"
5     android:textColor="#ff0000"
6     />

//定义宽度,fill_parent父类的宽度
android:layout_width="fill_parent"
//定义高度,wrap_content文本的高度
android:layout_height="wrap_content"
//设置文本内容 ,内容从values中strings.xml取,也可以直接写文本内容
android:text="@string/hello"
//设置字体大小
android:textSize="30sp"

b,通过代码生成TextView

1  TextView tv = new TextView(this);
2  tv.setText("你好!");
3  setContentView(tv);

2.设置TextView颜色

a.通过配置文件,这样写的效果是设置全部TextView中字的颜色

1 android:textColor="#00ff00"

b.通过Html.fromHtml设置

1  TextView tv = (TextView)findViewById(R.id.tv);
2  tv.setText(Html.fromHtml("<font color=red>Go 编程语言</font>是一个使得程序员更加有效率的开源项目"));

c.通过如下SpannableStringBuilder,可以设置具体的字体颜色

1         TextView tv = (TextView)findViewById(R.id.tv);
2         String str ="Go 编程语言是一个使得程序员更加有效率的开源项目"; 
3         SpannableStringBuilder style = new SpannableStringBuilder(str);
4         style.setSpan(new ForegroundColorSpan(Color.GRAY), 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
5         style.setSpan(new ForegroundColorSpan(Color.GREEN), 5, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
6         style.setSpan(new ForegroundColorSpan(Color.BLUE), 10, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
7         tv.setText(style);

findViewById必须在TextView中设置android:id属性,如

android:id="@+id/tv"

这样写会自动在R.java中生成如下代码

1 public static final class id {
2       public static final int tv=0x7f050000;
3 }

3.跑马灯效果:

1     android:singleLine="true"
2     android:focusable="true"
3     android:ellipsize="marquee"
4     android:marqueeRepeatLimit="marquee_forever"
5     android:focusableInTouchMode="true"

4.设置超链接,电话,email效果

android:autoLink="all"

android:autoLink几种选择:none,web,email,phone,map,all设置相应选项,android会调用相应的程序,web会调用浏览器打开相应链接,all是上面全部几个选项

原文地址:https://www.cnblogs.com/atyou/p/2726779.html