基础属性详解
- layout_width:组件的宽度
- layout_height:组件的高度
- id:为TextView设置一个组件id
- text:设置显示文本内容
- textColor:设置字体颜色
- textStyle:设置字体风格,三个可选值:normal(无效果),bold(加粗),ltalic(斜体)
- textSize:字体大小,单位一般是用sp
- background:控件的背景颜色,可以理解为填充整个控件的颜色,也可以是图片
- gravity:设置控件中内容的对齐方向,TextView中是中字,ImageView中是图片等等
activity_main.xml 中测试
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/tv_one"
android:text="@string/tv_one"
android:textColor="@color/black"
android:textStyle="italic"
android:textSize="30sp"
android:background="@color/red"
android:gravity="center_vertical"
android:layout_width="200dp"
android:layout_height="200dp"/>
</LinearLayout>
其中颜色,文本放在res目录下values文件中设置
colors.xml:
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="red">#FFFF0000</color>
</resources>
String.xml:
<resources>
<string name="app_name">Test</string>
<string name="tv_one">哈哈</string>
</resources>
带阴影的TextView
- androd:shadowColor:设置阴影颜色,需要与shadowRadius一起使用
- android:shadowRadius:设置阴影的模糊程度,设为0.1就变成字体颜色了,建议使用3.0
- android:shadowDx:设置阴影在水平方向的偏转,就是水平方向阴影开始的横坐标位置
- android:shadowDy:设置阴影在竖直方向阴影开始的纵坐标位置
测试:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/tv_one"
android:text="@string/tv_one"
android:textColor="@color/black"
android:textStyle="italic"
android:textSize="30sp"
android:gravity="center_vertical"
android:shadowColor="@color/red"
android:shadowRadius="3.0"
android:shadowDx="10.0"
android:shadowDy="10.0"
android:layout_width="200dp"
android:layout_height="200dp"/>
</LinearLayout>
实现跑马灯效果的TextView
- android:singleLine:内容单行显示
- android:focusable:是否可以获取焦点
- android:focusablelnTouchMode:用于控制视图在触摸模式下是否可以聚焦
- android:ellipsize:在哪里省略文本
- android:marqueeRepeatLimt:字幕动画重复的次数
需要获取到焦点才可以跑
可点击
android:clickable="true"
方法一:自定义一个MyTextView类
MyTextView.class:
package com.example.test;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
import androidx.annotation.Nullable;
public class MyTextView extends TextView { //需要定义构造方法
public MyTextView(Context context) {
super(context);
}
public MyTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean isFocused() {
return true;
}
}
activty_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<com.example.test.MyTextView
android:id="@+id/tv_one"
android:text="@string/tv_one"
android:textColor="@color/black"
android:textStyle="italic"
android:textSize="30sp"
android:gravity="center_vertical"
android:shadowColor="@color/red"
android:shadowRadius="3.0"
android:shadowDx="10.0"
android:shadowDy="10.0"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="200dp"/>
</LinearLayout>
方法二:加一个<requesFocus/
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/tv_one"
android:text="@string/tv_one"
android:textColor="@color/black"
android:textStyle="italic"
android:textSize="30sp"
android:gravity="center_vertical"
android:shadowColor="@color/red"
android:shadowRadius="3.0"
android:shadowDx="10.0"
android:shadowDy="10.0"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="200dp">
<!--请求焦点-->
<requestFocus/>
</TextView>
</LinearLayout>