TextView控件

  • 讲解
  • 基础属性介绍
  • 后台调用
  • 设置TextView的阴影
  • 设置TextView边框
  • 带图片(drawableXxx)的TextView
  • 实现跑马灯

讲解

        TextView作为Android最基础也是最常用的组件之一,他承担着文本的显示重任。要注意,其显示的文本内容是无法在界面直接被用户修改的。不过作为程序员,可以通过后台代码去修改TextView的内容和各项属性。另外要注意的是,TextView控件是需要放在容器里面的,比如LinearLayout容器(一般控件都要放在容器里面)。

基础属性介绍

属性

说明

id

设置一个组件id(唯一),通过findViewById()的方法获取到该对象,然后进行相关设置

layout_width

设置组件宽度,可以填充数字和Android提供的枚举值,Android提供的两个枚举值:match_parent:与父类宽度匹配(减去padding)(在Level 8之后,代替废弃的fill_parent),wrap_content:组件应该足够大到足以其内容(加上padding,当然不超过其父类)。

layout_height

设置组件高度,可以填充数字和Android提供的枚举值,Android提供的两个枚举值:match_parent:与父类高度匹配(减去padding)(在Level 8之后,代替废弃的fill_parent),wrap_content:组件应该足够大到足以其内容(加上padding,当然不超过其父类)。

text

设置显示的文本内容

background

设置背景颜色(或背景图片)

textColor

设置字体颜色

textStyle

设置字体样式 ,三个可选值:normal(无效果),bold(加粗),italic(斜体)

textSize

字体大小,单位一般用sp

maxHeight

设置文本框最大高度

gravity

内容的对齐方向

        示例:

<TextView
        android:id="@+id/tView1"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="修改"
        android:textColor="@color/white"
        android:textSize="90dp"
        android:textStyle="bold"
        android:gravity="center"
        android:background="@color/black"/>

后台调用

        Java后台通过ID调用。
        注意:Java会覆盖对应的TextView 原本内容。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView tv = findViewById(R.id.tView1);
        tv.setText("TextView1");
    }

设置TextView的阴影

属性

说明

shadowColor

设置字体阴影的颜色,单独设置没有用,需要跟shadowRadius一起使用

shadowRadius

设置字体阴影的模糊程度,一般设置3~5。

shadowDx

设置阴影在水平方向的平移

shadowDy

设置阴影在竖直方向的平移

        一般都是四个搭配使用。

<TextView
        android:id="@+id/tv_one"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:gravity="center"
        android:shadowColor="@color/black"
        android:shadowRadius="3.0"
        android:shadowDx="8"
        android:shadowDy="8"
        android:text="@string/tv_one"
        android:textColor="@color/purple_500"
        android:textSize="20dp"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp"
        tools:ignore="MissingConstraints" />

        效果图:

Android Textview设置箭头 安卓textview设置文本_java

设置TextView边框

        首先要写Drawable文件:

        圆角带边框:

<!-- ic_txt_radiuborder.xml -->
 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- 设置透明背景色 -->
    <solid android:color="#87CEEB" />

    <!-- 设置一个黑色边框 -->
    <stroke
        android:width="2px"
        android:color="#000000" />
    <!-- 设置四个圆角的半径 -->
    <corners
        android:bottomLeftRadius="15px"
        android:bottomRightRadius="15px"
        android:topLeftRadius="15px"
        android:topRightRadius="15px" />
    <!-- 设置一下边距,让空间大一点 -->
    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />
</shape>

        矩形带边框:

<!-- ic_txt_rectborder.xml -->
 <?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android" >

<!-- 设置一个黑色边框 -->
<stroke android:width="2px" android:color="#000000"/>
<!-- 渐变 -->
<gradient
    android:angle="270"
    android:endColor="#C0C0C0"
    android:startColor="#FCD209" />
<!-- 设置一下边距,让空间大一点 -->
<padding
    android:left="5dp"
    android:top="5dp"
    android:right="5dp"
    android:bottom="5dp"/>

</shape>

将TextView的blackground属性设置成上面这两个Drawable:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
    android:id="@+id/txtOne"
    android:layout_width="100dp"
    android:layout_height="64dp"
    android:textSize="18sp"
    android:gravity="center"
    android:background="@drawable/ic_txt_rectborder"
    android:text="矩形带边框的TextView" />

<TextView
    android:id="@+id/txtTwo"
    android:layout_width="100dp"
    android:layout_height="64dp"
    android:layout_marginTop="10dp"
    android:textSize="18sp"
    android:gravity="center"
    android:background="@drawable/ic_txt_radiuborder"
    android:text="圆角带边框的TextView" />
</LinearLayout>

Android Textview设置箭头 安卓textview设置文本_xml_02

带图片(drawableXxx)的TextView

        在日常生活中,我们经常会看到这种带图片和文字的模型,其实这里是可以通过drawableXxx实现:

Android Textview设置箭头 安卓textview设置文本_android_03

属性

说明

drawableTop

图片显示在内容上方

drawableBottom

图片显示在内容下方

drawableLeft

图片显示在内容左侧

drawableRight

图片显示在内容右侧

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:drawableTop="@mipmap/ic_launcher"
    android:drawablePadding="10dp"
    android:gravity="center"
    android:text="安卓12.0" />

</LinearLayout>

Android Textview设置箭头 安卓textview设置文本_xml_04

实现跑马灯

属性

说明

singleLine

内容单行显示

focusable

设置是否可以获取焦点 ,如果是跑马灯,则需要设置为true,让该textView去获取焦点

focusableTouchMode

控制试图在触摸模式下是否可以聚焦,就是在触摸某个控件,会先将焦点移动到被触摸的控件上,然后需要再触摸该控件才会响应单击事件。

ellipsize

设置省略文本. “start”:省略号显示在开头;“end”:省略号显示在结尾;“middle”:省略号显示在中间;“marquee”:以横向滚动方式显示(需要获取焦点)

marqueeRepeatLimit

字母动画重复次数,如果要一直跑的话,那就要设置marquee_forever

        设置跑马灯,这些设置还不够,因为跑马灯是需要在有焦点的时候才能跑起来,让文本获取焦点主要有下面三种方式:

  1. 添加clickable=true 属性,这种只会在特殊情况下用,因为,这种需要你点击一下才能起效果
  2. 重写TextView的ifFocused方法,设置返回为True。然后使用你重写的文本框架。
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
import androidx.annotation.Nullable;
// @SuppressLint 解决:This custom view should extend androidx.appcompat.widget.AppCompatTextView instead
@SuppressLint("AppCompatCustomView")
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;
    }
}
<com.example.androidstudy.MyTextView
        android:id="@+id/tv_one"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:gravity="center"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="false"
        android:clickable="true"
        android:text="@string/tv_one"
        android:textColor="@color/purple_500"
        android:textSize="20dp"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="0dp">
    </com.example.androidstudy.MyTextView>
  1. 在<TextView> </TextView> 中加入 <requestFocus/>
<TextView
        android:id="@+id/tv_one"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:gravity="center"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="false"
        android:clickable="true"
        android:text="@string/tv_one"
        android:textColor="@color/purple_500"
        android:textSize="20dp"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="0dp">
        <requestFocus/>
    </TextView>

        在这里要注意:android:singleLine 官方建议用maxLines 替换。但是maxLines折行算法有一些坑