本文已参与「新人创作礼」活动,一起开启掘金创作之路
TextView
基本属性
1.textColor属性:前俩位表示透明度,后俩位分别为红绿蓝
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:textColor="#FF0000FF"/>
2.textStyle设置字体的风格, 三个可选值:normal(无效果),bold(加粗),italic(斜体)
3.backgroud:控件的背景颜色,可以理解为填充整个空间的颜色,可以是图片
4.gravity:设置控件中内容对齐的方向,TextView中是文字,ImageView中是图片等
带阴影的TextView
shadowColor:设置阴影颜色,需要与shadowRadius一起使用
shadowRadius:设置阴影的模糊程度,设为0.1就变成了字体颜色,建议使用3.0
shadowDx:设置阴影在水平方向上的偏移
shadowDy:设置阴影在竖直方向上的偏移
实现跑马灯效果的TextView
singleLint:内容当行显示
focusable:是否可以获得焦点
focusableInTouchMode:用于控制视图在触摸模式下是否可以聚焦
ellipsize:省略号出现在哪里
marqueeRepeatLimit:字幕动画重复的次数
第一种实现方式
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="赵喂喂喂喂喂喂喂喂喂喂喂喂喂"
android:textSize="20sp"
android:singleLine="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:ellipsize="marquee"
android:clickable="true"
android:textColor="#FF0000FF"/>
在11行实现了clickable=true,点击跑马灯就跑起来的,但是我们往往是想要跑马灯在应用运行起来就开始跑,所以使用另外俩种方式
第二种实现方式,自定义TextView
package com.example.lesson1_textview;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
import androidx.annotation.Nullable;
public class My_TextView extends TextView {
public My_TextView(Context context) {
super(context);
}
public My_TextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public My_TextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean isFocused() {
return true;
}
}
第三种实习方式,在控件中聚焦
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我我我我我我我我我我我我我我我我"
android:textSize="20sp"
android:singleLine="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:ellipsize="marquee"
android:clickable="true"
android:textColor="#FF0000FF">
<requestFocus/>
</TextView>
以上几种方式均为Textview 的基本运用