TextView
- 基本属性
- layout width:组件的宽度。
android:layout_width="match_parent"
与父容器相同
android:layout_width="wrap_content"
随内容而变
android:layout_width="xxdp"
自定义 - layout_height:组件的高度 。
android:layout_height="match_parent"
与父容器相同
android:layout_height="wrap_content"
随内容而变
android:layout_height="xxdp"
自定义 - id:为TextViewi设置一个组件id 。
android:id="@+id/xxx"
用于控件绑定 - text:设置显示的文本内容 。
android:text="xxx"
TextView的显示内容 - textColor:设置字体颜色
android:textColor="#FFFFFFFF"
每二位一组分别对应透明度、红、绿、蓝。
规范使用方式在values下的colors.xml中添加<color name="colorWhite">#FFFFFFFF</color>
后android:textColor="@color/colorWhite"
- textStyle:设置字体风格,三个可选值:normal(无效果),bold加粗),italic(斜体) 。
android:textStyle=xxx""
- textSize:字体大小,单位一般是用sp 。
android:textSize="xxxsp"
- background:控件的背景颜色,可以理解为填充整个控件的颜色,可以是图片。
android:background="@color/xxx"
android:background="@drawable/xxx"
- gravity:设置控件中内容的对齐方向,TextView中是文字,ImageView中是图片等等。
android:gravity="xxx"
控件内容的对齐方向
android:layout_gravity="xxx"
整个控件与父容器的对齐方向
拓展
- 带阴影的TextView
1.android:shadowColor="@color/xxx"
设置阴影颜色,需要与shadowRadius-一起使用
2.android:shadowRadius=""
设置阴影的模糊程度,设为0.1就变成字体颜色了,建议使用3.0
3.android:shadowDx=""
设置阴影在水平方向的偏移,就是水平方向阴影开始的横坐标位置
4. android:shadowDy=""
设置阴影在竖直方向的偏移,就是竖直方向阴影开始的纵坐标位置
- TextView的跑马灯效果
text内容一般要长于容器宽度
1.android:singleLine="true"
内容单行显示
2.android:focusable="true"
是否可以获取焦点
3.android:focusablelnTouchMode="true"
用于控制视图在触摸模式下是否可以聚焦
4.android:ellipsize="marquee"
跑马灯效果
5.android:marqueeRepeatLimit="marquee_forever"
跑马灯动画重复的次数
6. <requestFocus/>
获取焦点,也可以通过自定义View重写isFocused()函数为return true;
实现
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" >
<requestFocus/>
</TextView>
</androidx.constraintlayout.widget.ConstraintLayout>