一、TextView:基础属性详解

  1. 1.layout_width:组件的宽度(wrap_content:自定义宽度,文本多宽,实际多宽)<br /> 2.layout_height:组件的高度(match_parent:占满,盒子有多高,实际就有多高)<br /> 3.id:为TextView设置一个组件id@id/tv_one)<br /> 4.text:设置显示的文本内容(@String/tv_one)<br /> 5.textColor:设置字体颜色(@color/red)<br /> 6.textStyle:设置字体风格,三个可选值;normal(无效果)、bold(加粗)、italic(斜体)<br /> 7.textSize:字体大小,单位一般是用sp(手机大小的适配,当放大缩小时,字体根据屏幕放大缩小)<br /> 8.background:控件的背景颜色,可以理解为填充整个控件的颜色,可以是图片<br /> 9.gravity:设置控件种内容的对齐方向,TextView中是文字,ImageView中是图片等等(center

二、带阴影的TextView

  1. 1.android:shadowColor:设置阴影颜色,需要与shadowRadius一起使用<br /> 2.android:shadowRadius:设置阴影的模糊程度,设为0.1就变成字体颜色了,建议使用3.0<br /> 3.android:shadowDx:设置阴影在水平方向的偏移,就是水平方向阴影开始的横坐标位置<br /> 4.android:shadowDy:设置阴影在竖直方向的偏移,就是竖直方向阴影开始的纵坐标位置
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity"
  8. android:gravity="center"
  9. >
  10. <TextView
  11. android:layout_width="200dp"
  12. android:layout_height="200dp"
  13. android:text="学习"
  14. android:textStyle="italic"
  15. android:textSize="30sp"
  16. android:shadowColor="#ffff0000"
  17. android:shadowRadius="3.0"
  18. android:shadowDx="10.0"
  19. android:shadowDy="10.0"
  20. />
  21. </LinearLayout>

image.png

三、实现跑马灯效果的TextView

  1. 1.android:singleLine:内容单行显示<br /> 2.android:focusable:是否可以获取焦点<br /> 3.android:focusableInTouchMode:用于控制视图在触摸模式下是否可以聚焦<br /> 4.android:ellipsize:在哪里省略文本<br /> 5.android:marqueeRepeatLimit:字幕动画重复的次数

方式一:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity"
  8. android:gravity="center"
  9. >
  10. <TextView
  11. android:layout_width="match_parent"
  12. android:layout_height="200dp"
  13. android:text="学习 学习 学习 学习 学习 学习 学习 学习 学习 学习 学习啊 11!!!"
  14. android:textStyle="italic"
  15. android:textSize="30sp"
  16. android:shadowColor="#ffff0000"
  17. android:shadowRadius="3.0"
  18. android:shadowDx="10.0"
  19. android:shadowDy="10.0"
  20. android:singleLine="true"
  21. android:ellipsize="marquee"
  22. android:marqueeRepeatLimit="marquee_forever"
  23. android:focusable="true"
  24. android:focusableInTouchMode="true">
  25. <requestFocus/>
  26. </TextView>
  27. </LinearLayout>

方式二:
  1. 自定义MyTextView这个类,继承TextView<br /> 1.activity_main.xml布局文件
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:gravity="center"
  8. >
  9. <com.example.test.MyTextView
  10. android:layout_width="match_parent"
  11. android:layout_height="200dp"
  12. android:text="学习 学习 学习 学习 学习 学习 学习 学习 学习 学习 学习啊 11!!!"
  13. android:textStyle="italic"
  14. android:textSize="30sp"
  15. android:shadowColor="#ffff0000"
  16. android:shadowRadius="3.0"
  17. android:shadowDx="10.0"
  18. android:shadowDy="10.0"
  19. android:singleLine="true"
  20. android:ellipsize="marquee"
  21. android:marqueeRepeatLimit="marquee_forever"
  22. android:focusable="true"
  23. android:focusableInTouchMode="true"/>
  24. </LinearLayout>
  1. 2.MyTextView代码文件
  1. package com.example.test;
  2. import android.content.Context;
  3. import android.util.AttributeSet;
  4. import android.widget.TextView;
  5. import androidx.annotation.Nullable;
  6. public class MyTextView extends TextView {
  7. public MyTextView(Context context) {
  8. super(context);
  9. }
  10. public MyTextView(Context context, @Nullable AttributeSet attrs) {
  11. super(context, attrs);
  12. }
  13. public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  14. super(context, attrs, defStyleAttr);
  15. }
  16. @Override
  17. public boolean isFocused() {
  18. return true;
  19. }
  20. }
  1. 3.实现图(跑马灯形式)<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/1574587/1621505279363-a7a2a72c-2b1a-468a-b8ae-39ba2d2e10e7.png#clientId=uc0016776-9eb6-4&from=paste&height=596&id=ubecca789&margin=%5Bobject%20Object%5D&name=image.png&originHeight=596&originWidth=1203&originalType=binary&size=123972&status=done&style=none&taskId=u799fb660-bac1-4171-9d89-9a5932741dd&width=1203)

方式三:
  1. 点击之后变成跑马灯形式(很少用这种方式)
  1. android:clickable="true"