Android:TextView属性
来源:互联网 发布:淘宝网儿童雨衣 编辑:程序博客网 时间:2024/06/05 07:45
今天学习了一下TextView的属性,参考了资料,总结了这些。在Android中有些控件直接或间接的继承了TextView,继承了它,TextView有的方法,它就可以使用。
一、TextView常用属性如下:
android:text 设置文本内容
android:textSize 设置文字大小,推荐度量单位sp,如“18sp”
android:textColor 设置文本颜色
android:gravity 设置文本在TextView这个控件中的重心位置,如设置成“center”,文本将居中显示
android:singleLine 设置单行显示
android:ellipsize 设置当文字过长时,该控件该如何显示。与android:singleLine一起使用时效果才显示的出来,有如下值可以设置:”start”— 省略号显示在开头;”end”— 省略号显示在结尾;”middle”— 省略号显示在中间; ”marquee” — 以跑马灯的方式显示(动画横向移动) ;"none" --- 超出的文字将不显示
二、TextView其他属性:
android:textColorHint 设置提示信息文字的颜色,与hint属性一起使用 ;设置了hint属性的文字颜色默认为灰色
android:hint 当文本为空时显示的文字提示信息,可通过textColorHint设置提示信息的颜色。此属性在EditView中使用,但是这里也可以用。
android:textColorLink 文字链接的颜色
android:autoLink 设置当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接。可选值(none/web/email/phone/map/all) ,设置多个时,用'|'分隔
android:linksClickable 设置链接是否可以被点击,前提是设置了autoLink这个属性
android:textStyle 设置字形 [bold(粗体) 0, italic(斜体) 1, bolditalic(又粗又斜) 2] 可以设置一个或多个,用“|”隔开
android:password 以小数点".”显示文本,起到隐藏密码的作用,在EditText控件中使用较多,这里也可以使用
android:inputType 设置文本的类型,用于帮助输入法显示合适的键盘类型。在EditView中再详细说明,这里无效果
android:editable 设置是否可编辑
android:drawableBottom 在text的下方输出一个drawable,如图片。如果指定一个颜色的话会把text的背景设为该颜色,并且同时和background使用时覆盖后者
android:drawableLeft 在text的左边输出一个drawable,如图片
android:drawableRight 在text的右边输出一个drawable
android:drawableTop 在text的正上方输出一个drawable
android:drawablePadding 设置text与drawable(图片)的间隔,与drawableLeft、drawableRight、drawableTop、drawableBottom一起使用,可设置为负数,单独使用没有效果。
android:digits 设置允许输入哪些字符。如“0123456789.+-*/% ”
android:textAllCaps设置文本中的字母是否大写显示
设置文本的阴影:
android:shadowColor指定文本阴影的颜色,需要与shadowRadius属性一起使用。
android:shadowDx 设置阴影横向偏移量。
android:shadowDy 设置阴影纵向偏移量。
android:shadowRadius 设置阴影的半径。设置为0.1就变成字体的颜色了,一般设置为3.0的效果比较好。
使用以上四个属性可以做出好看的文本图片,其他阴影效果见 http://blog.csdn.net/hewence1/article/details/39993415
<!-- 设置字体为25sp,文本框结尾处显示一张图片 --> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:drawableEnd="@drawable/ic_launcher" android:text="我和Android" android:textSize="25sp" /> <!-- 设置中间省略, 所有字母大写 --> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="middle" android:singleLine="true" android:text="我和Java我和Java我和Java我和Java我和Java我和Java我和Java我和Java" android:textAllCaps="true" /> <!-- 对邮件、电话增加链接 --> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:autoLink="email|phone" android:singleLine="true" android:text="我的邮件是ch_95@163.com,电话是10023469018" /> <!-- 设置链接不可点击 --> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:autoLink="phone" android:linksClickable="false" android:singleLine="true" android:text="10123469018" /> <!-- 设置文字颜色 、大小,并使用阴影 --> <TextView android:layout_width="match_parent" android:layout_height="35dp" android:shadowColor="#000000" android:shadowDx="10.0" android:shadowDy="8.0" android:shadowRadius="1.0" android:text="我是测试文字" android:textColor="#ff0000" android:textSize="18sp" /> <!-- 测试密码框 --> <TextView android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:password="true" android:text="hello" />
TextView的其他属性见: http://blog.csdn.net/doris_d/article/details/51364408
三、用TextView实现跑马灯的效果
1)如果是单个TextView在XML文件中使用一下五个属性即可
android:singleLine="true" // 单行显示 android:focusable="true"// 让文本处于focus状态 android:focusableInTouchMode="true" // 可以通过touch来获得focus android:ellipsize="marquee" // 当文字过长时,设置marquee让文本以跑马灯的形式显示 android:marqueeRepeatLimit="marquee_forever"// 让文字一直循环下去2)但是多个TextView都要实现跑马灯效果,这时候仅仅这么设置只有第一个有效果,其他没有效果。由于默认的跑马灯效果是需要TextView的属性isFocused为true的时候才会跑动的,而默认情况下,只有一个TextView处于focused状态,解决方法就是让所有需要实现跑马灯效果的TextView都处于focused状态,因此要继承TextView类,于是新建一个MyTextView类,调用三个构造方法,并重写isFocused()方法,该方法返回true。
public class MyTextView extends TextView {public MyTextView(Context context) {super(context);}public MyTextView(Context context, AttributeSet attrs) {super(context, attrs);}public MyTextView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}@Overridepublic boolean isFocused() {return true;}}
<com.zhixing.textviewdemo.MyTextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="20dp" android:singleLine="true" android:focusable="true" android:focusableInTouchMode="true" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" android:text="我是文本我是文本我是文本我是文本我是文本我是文本我是文本我是文本" /> <com.zhixing.textviewdemo.MyTextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="20dp" android:layout_marginTop="10dp" android:singleLine="true" android:focusable="true" android:focusableInTouchMode="true" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />
这样就可以让多个文本都实现跑马灯效果
- Android TextView属性详解
- Android TextView 属性
- android TextView属性
- android TextView属性大全
- TextView android:ellipsize 属性
- android之TextView属性
- android TextView background属性
- Android TextView属性详解
- android TextView属性大全
- android textview属性
- 【Android】TextView 属性详解
- android TextView属性详解
- 【android】TextView属性大全
- Android TextView属性详解
- android textView属性
- Android----TextView属性大全
- Android TextView属性详解
- android-TextView属性
- ffmpeg实现dxva2硬件加速
- 2016级新生程序设计全国邀请赛个人题解 [未完待续..]
- MMU内存管理单元相关知识点总结
- 【转】 Android 属性动画(Property Animation) 完全解析 (上)
- Leetcode||28. Implement strStr()
- Android:TextView属性
- 算法调试心得
- Android开发艺术探索笔记(10)- View的弹性滑动
- Scala 的嵌套方法的定义与递归
- NOIP2016-PJ-T1
- Java 四种线程池
- 欢迎使用CSDN-markdown编辑器
- 第14周 项目2 - 二叉树排序树中查找的路径
- 133. Clone Graph