(1)TextView单行滚动显示
代码如下:
代码中设置
singleLine单行显示,
ellipsize跑马滚动,
marqueeRepeatLimit滚动次数
focusable,focusableInTouchMode焦点为true,实测两项都要加上,如果不加上focusableInTouchMode无效。
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:singleLine="true"
android:ellipsize="marquee"//设置start,end,marquee.start,end是指的省略号的位置。
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="Menot working hard?\nYea right picture that with a kodakAnd better yet"/>
view类中源码:
publicvoidsetFocusable(booleanfocusable) {
if(!focusable) {
setFlags(0,FOCUSABLE_IN_TOUCH_MODE);
}
setFlags(focusable?FOCUSABLE:NOT_FOCUSABLE,FOCUSABLE_MASK);
}
publicvoidsetFocusableInTouchMode(booleanfocusableInTouchMode) {
//Focusable in touch mode should always be set before the focusableflag
//otherwise, setting the focusable flag will trigger afocusableViewAvailable()
//which, in touch mode, will not successfully request focus on thisview
//because the focusable in touch mode flag is not set
setFlags(focusableInTouchMode?FOCUSABLE_IN_TOUCH_MODE: 0, FOCUSABLE_IN_TOUCH_MODE);
if(focusableInTouchMode) {
setFlags(FOCUSABLE,FOCUSABLE_MASK);
}
}
注释提示到setFocusableInTouchMode在代码中必须要在setFocusable前设置。
注意:
实测AlertDialog和Activity中可以实现的。但在通知栏(Notification)中没实现
由于通知栏是用的RemoteViews,跑马灯效果并不能在通知栏实现。同理,窗口小部件也不行。(个人看法)。
(2)TextView xml属性:
android:drawablePadding
设置text与drawable(图片)的间隔,与drawableLeft、drawableRight、drawableTop、drawableBottom一起使用,可设置为负数,单独使用没有效果。
android:ellipsize
ellipsis是省略号的意思。
设置当文字过长时,该控件该如何显示。有如下值设置:”start”—?省略号显示在开头;”end”——省略号显示在结尾;”middle”—-省略号显示在中间;”marquee”——以跑马灯的方式显示(动画横向移动)
android:singleLine单行显示。
android:marqueeRepeatLimit
在ellipsize指定marquee的情况下,设置重复滚动的次数,当设置为marquee_forever时表示无限次。
android:ems,android:masEms,android:minEms
设置TextView的宽度为N个字符的宽度。测试为大约为一个汉字字符宽度,并且最后一个字符后会加上一小段距离。(大约半个汉字)。这个宽度基准很模糊。实测和android:width=”wrap_content”配合使用才生效。
如果字符串大于N是会换行的。
android:maxLength
限制显示的文本长度,超出部分不显示。实测感觉就是截断。
如果字符串大于N是不会换行的。
而且不是用宽度来衡量的,和android:ems要区分。
例如:android:maxLength设置为3.
那么
“你好吗朋友”显示”你好吗”
“hellofriend”显示”hel”
“123456789”显示”123”
androi:ems设置为3
显示为两行:
你好吗
朋友
hellofri
end
123456
789
android:lines,android:maxLines,android:minLines一般会用到maxLines。
android:lineSpacingExtra设置行间距。
android:lineSpacingMultiplier设置行间距的倍数。如”1.2”
android:shadowColor指定文本阴影的颜色,需要与shadowRadius一起使用。
android:shadowDx设置阴影横向坐标开始位置。
android:shadowDy设置阴影纵向坐标开始位置。x,y都是0就是发光效果,不为0就是阴影效果。
android:shadowRadius设置阴影的半径。设置为0.1就变成字体的颜色了,一般设置为3.0的效果比较好。
android:textAppearance
设置文字外观。
“android:textAppearance=“?android:attr/textAppearanceLargeInverse”
这里引用的是系统自带的一个外观,?表示系统是否有这种外观,否则使用默认的外观。
android:textColorHighlight被选中文字的底色,默认为蓝色
android:textColorHint设置提示信息文字的颜色,默认为灰色。与hint一起使用。
android:textColorLink文字链接的颜色.
android:textScaleX设置文字之间间隔,默认为1.0f。
android:textStyle
设置字形[bold(粗体)0, italic(斜体)1, bolditalic(又粗又斜)2]可以设置一个或多个,用“|”隔开
android:typeface
设置文本字体,必须是以下常量值之一:normal0, sans 1, serif 2, monospace(等宽字体)3
android:height
设置文本区域的高度,支持度量单位:px(像素)/dp/sp/in/mm(毫米),注意不layout_height。
android:maxHeight设置文本区域的最大高度
android:minHeight设置文本区域的最小高度
android:width
设置文本区域的宽度,支持度量单位:px(像素)/dp/sp/in/mm(毫米),注意不是layout_width。
android:maxWidth设置文本区域的最大宽度
android:minWidth设置文本区域的最小宽度
android:includeFontPadding
关联方法:setIncludeFontPadding(boolean)
属性说明:设置文本是否包含顶部和底部额外空白,默认为true。
android:textAllCaps
关联方法:setAllCaps(boolean)
属性说明:设置文本全为大写。值为"true"或"false"。
(3)setTypeface设置字体,Typeface类学习
Paint类,TextView类有这个api
就是字体。
【基本语法】publicTypeface setTypeface ( Typeface typeface)
其中,参数typeface为字体样式,具有如下几种取值。
Typeface.DEFAULT:默认字体。
Typeface.DEFAULT_BOLD:加粗字体。
Typeface.MONOSPACE:monospace字体。
Typeface.SANS_SERIF:sans字体。
Typeface.SERIF:serif字体
Android系统默认支持三种字体,分别为:“sans”,“serif”, “monospace",除此之外还可以使用其他字体文件(*.ttf)
方法一:XML中使用android默认字体
Android:text="Hello,World"
Android:typeface="sans"
Android:textSize="20sp"/>
Android:text="Hello,World"
Android:typeface="serif"
Android:textSize="20sp"/>
Android:text="Hello,World"
Android:typeface="monospace"
Android:textSize="20sp"/>
方法二:在Android中可以引入其他字体,首先要将字体文件保存在assets/fonts/目录下
1. Android:text="Hello,World"
Android:textSize="20sp"/>
2. java程序中引入其他字体关键代码
//得到TextView控件对象
TextViewtextView =(TextView)findViewById(R.id.custom);
//将字体文件保存在assets/fonts/目录下,创建Typeface对象
//getAssets()返回的AssetManager对象
TypefacetypeFace=Typeface.createFromAsset(getAssets(),"fonts/HandmadeTypewriter.ttf");
//使用字体
textView.setTypeface(typeFace);
在实际使用中,字体库可能存在于SD卡上,可以采用createFromFile()来替代createFromAsset。
(4)用TextView同时显示文字和图片
继承于TextView的Button,EditText等控件自然也有以下属性。可以同时显示文字和图片。
使用android:drawableLeft=”@drawable/xxx”
drawableLeft,drawableRight,drawableTop,drawableBottom.
使用方式和之前总结的Checkbox自定义图片有点类似。
这样可以少使用控件,有些情况下可以减少嵌套。
(5)TextView显示多行
特点:多行文字,文字大小相同,一般是左对齐。上下行间距可调整。
<TextView
android:text=”揽件\n快递\n预约\n快递”
android:lineSpacingExtra=”10dp”/>
用\n来换行.
lineSpacingExtra属性代表的是行间距,他默认是0,是一个绝对高度值。
同时还有lineSpacingMultiplier属性,它代表行间距倍数,默认为1.0f,是一个相对高度值。
当然了这两条属性可以同时使用,查看源码可以知道,他们的高度计算规则为mTextPaint.getFontMetricsInt(null)* 行间距倍数+行间距。
(6)TextView显示一行字符串,不同的字体大小和颜色的实现
mTextView=(TextView)findViewById(R.id.tv);
StringmString=String.format("售价%1$s 门市价%2$s",20,25);
SpannableStringBuildermBuilder=newSpannableStringBuilder(mString);
intz=mString.indexOf("门");
mBuilder.setSpan(newAbsoluteSizeSpan(80), 0,z, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
mTextView.setText(mBuilder);