Android 基础控件
TextView 文本框控件
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World TextView This is our first meetingThis we first met!"
android:textColor="@color/design_default_color_error"
android:textSize="20sp"
android:textStyle="bold"
android:maxLines="1"
android:maxLength="50"
android:ellipsize="end"/>
常用属性
- android:text 设置文本内容。可以直接写入字符串,也可以通过res/values/strings.xml中获取(推
荐:为了避免多语言(国际化)的文字翻译问题) - android:textColor 设置文字颜色。
- android:textSize 设置字体大小。 一般大小单位为sp除非不允许与系统设置字体匹配,否则慎用dp作为字体单位
- android:textStyle 设置字体样式。
- 参数有
- bold: 粗体
- italic: 斜体
- normal: 正常
- android:maxLines 最大行数。
- android:maxLength 最大字符数。
- android:ellipsize 设置省略号位置或走马灯效果。
- 参数有
- end: 后省略 省略号显示在结尾 “abc…”
- middle: 中间省略 省略号显示在中间 “ab…xyz”
- start: 前省略 省略号显示在开头 “…abc”
- none:即默认,没有任何效果
- marquee: 跑马灯效果(需要额外处理)
marquee 设置跑马灯marquee这个值时并不能使文字直接滚动起来。必须要设置这两个属性:
android:singleLine=“true”
android:focusableInTouchMode=“true”
也可以设置其他属性
android:marqueeRepeatLimit=“marquee_forever” 设置为重复走动
android:singleLine=“true” 单行文字**
注意:也可以通过Java代码实现跑马灯效果,需要在代码中设置:在TextView中添加 android:singleLine=“true”
开启跑马灯
textView.setSelected(true);
关闭跑马灯
textView.setSelected(false);
TextView文本阴影
android:shadowColor 设置阴影颜⾊,需要与shadowRadius⼀起使⽤哦!
android:shadowRadius 设置阴影的模糊程度,设为0.1就变成字体颜⾊
了,建议使⽤3.0
android:shadowDx 设置阴影在⽔平⽅向的偏移,就是⽔平⽅向阴影开始的横坐标位置(默认阴影向右)
android:shadowDy 设置阴影在竖直⽅向的偏移,就是竖直⽅向阴影开始的纵坐标位置(默认阴影向下)
TextView⽂字带图⽚
主要是通过TextView的4个属性来设置不同位置的⽂字旁图⽚
android:drawableTop(上)
android:drawableButtom(下)
android:drawableLeft(左)
android:drawableRight(右)
另外,也可以使⽤drawablePadding来设置图⽚与⽂字间的间距
TextView属性识别链接类型
android:autoLink=“email”
TextView字间距和行间距
字间距:
android:textScaleX 控制字体水平方向的缩放,默认值1.0f,值是float
Java中setScaleX(2.0f);
行间距:
android:lineSpacingExtra 设置行间距,如"3dp" android:lineSpacingMultiplier:设置行间距的倍数,如"1.2"
Java代码中可