android TextView控件

来源:互联网 发布:mac flash插件错误 编辑:程序博客网 时间:2024/05/01 04:39

转至:http://blog.csdn.net/ygc87/article/details/6823235

1)设置文本是否包含顶部和底部的额外空白
在xml文件的TextView控件中加入如下属性:

[java] view plain copy
 print?
  1. android:includeFontPadding="false"   

 

2)有时直接引用资源文件不起作用(例如:textView.setTextColor(R.color.red)),可做如下处理:

[java] view plain copy
 print?
  1. textView.setTextColor(context.getResources().getColor(R.color.red));  

3)textview中可以显示多行,但是行数是有限制的,比如最多两行,如果第二行依然显示不下,显示singleline的效果

[html] view plain copy
 print?
  1. android:maxLines="2"   


4)文字的雕刻效果

[html] view plain copy
 print?
  1. <TextView  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="wrap_content"  
  4.     android:background="@drawable/title_bg"  
  5.     android:gravity="center_vertical|center_horizontal"  
  6.     android:shadowColor="#000000"  
  7.     android:shadowDx="0"  
  8.     android:shadowDy="-2"  
  9.     android:shadowRadius="0.1"  
  10.     android:text="雕刻效果的文字"  
  11.     android:textColor="#ffffff"  
  12.     android:textSize="28px" />  


 

 

5)跑马灯效果

[html] view plain copy
 print?
  1. <!--  
  2.     layout_width属性要小于文字的宽度,android:singleLine="true"以及  
  3.     android:focusable="true"属性也必须的。当TextView获得焦点时,会出现跑马灯效果。  
  4. -->  
  5. <TextView android:layout_width="50px" android:layout_height="wrap_content"  
  6.     android:singleLine="true" android:focusable="true" android:text="跑马灯效果 "  
  7.     android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" />  


6)中文字体加粗效果

[java] view plain copy
 print?
  1. ((TextView) findViewById(R.id.test)).getPaint().setFakeBoldText(true);  


 7)点击时TextView中文字颜色变化

首先建立res\color文件夹,然后创建textview_colors_selector.xml文件,内容如下:

 

[html] view plain copy
 print?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <item android:state_selected="true" android:color="#3399FF"/>  
  5.     <item android:state_focused="true" android:color="#3399FF"/>  
  6.     <item android:state_pressed="true" android:color="#3399FF"/>  
  7.     <item android:color="#FFFFFF"></item>  
  8.   
  9. </selector>  


然后在main.xml文件中指定TextView的textColor属性:

[html] view plain copy
 print?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_gravity="center"  
  11.         android:clickable="true"  
  12.         android:text="点击我会变颜色的哦!"  
  13. <span style="color:#ff0000;">        android:textColor="@color/textview_colors_selector"  
  14. </span>        android:textSize="24sp" />  
  15.   
  16. </LinearLayout>  


正常状态:

 

点击状态:

 

0 0
原创粉丝点击