1 创建TextView对象有两种方法:
1.1 在程序中创建TextView对象;
TextView tv=new TextView(this);
tv.setText("您好");
setContentView(tv);
1.2 在XML布局文件中使用;(推荐使用这种方法)
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="你好"
/>
设置字体的大小通常推荐使用sp作为单位。
设置宽度或者高度属性时使用 dip作为单位。
2 dp sp和px的区别:
dp也就是dip,和sp基本类似,如果设置表示长度、高度、等属性时可以使用dp或者sp。但是如果设置字体时,需要使用sp。dp是与密度无关的,sp除了与密度无关,还与scale无关。如果屏幕密度为160,这时dp和sp和px是一样的。1dp=1sp=1px。但是如果使用px作单位,如果屏幕大小不变,而屏幕密度变成了320.那么原来 TextView的宽度设成160px,在密度为320的3.2寸屏幕里看要比在密度为160的3.2寸屏幕上看短了一半。但如果设置成160dp或者160sp的话,系统会自动将width属性值设置成320px。也就是160*320/160。其中320/160可称为密度比例因子,也就是说,如果使用dp或者sp,系统会根据屏幕密度的变化自动进行转换。
px:表示屏幕实际的像素。
3 设置字体颜色方法
3.1 android:textColor="#00FF00"
3.2
TextView tv=(TextView)findViewById(R.id.tv);
tv.setText(Html.fromHtml("我的Android开发学习笔记"));
上述代码可以改变TextView控件中局部文本的颜色。
3.3
String str="欢迎大家学习Android开发";
SpannableStringBuilder style=new SpannableStringBuilder(str);
style.setSpan(new ForegroundColorSpan(Color.RED),0,6,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
style.setSpan(newForegroundColorSpan(Color.GREEN),6,13,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
style.setSpan(new ForegroundColorSpan(Color.BLUE),13,15,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(style);
4 设置超链接:
android:autoLink设置是否当文本为URL链接/email/电话/map时,文本显示为可点击的链接。
android:autoLink="all"
5 跑马灯效果
android:ellipsize设置当文字过长时,该控件该如何显示。有如下值设置
“start”----省略号显示在开头。
“end” ----省略号显示在结尾。
“middle”----省略号显示在中间。
“marquee”-----以跑马灯的方式现实。
当设置为marquee时,android:marqueeRepeatLimit设置重复滚动的次数,当设置为 marquee_forever时表示无限次。android:focusableInTouchMode:是否在触摸模式下获得焦点。android:focusable控件是否能够获取焦点。
实例: android:id="@+id/tv" android:autoLink="all" android:layout_width="fill_parent" android:layout_height="wrap_content" android:singleLine="true" android:textSize="20sp" android:textColor="#00FF00" android:focusable="true" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" android:focusableInTouchMode="true" android:text="欢迎大家学习Android开发。我的博客地址:http://blog.csdn.net/donggua199925我的电话15823008888" />