TextView

发布于 2022-03-31  57 次阅读


TextView 是 Android 里用来显示文本的控件,比较基础的控件,但也是用法颇多的控件,不要因为基础就觉的简单,本篇我们会详细介绍它的各种用法。

基础属性

id为TextView设置一个组件id,根据id,我们可以在Java代码中通过findViewById()的方法获取到该对象,然后进行相关属性的设置,又或者使用RelativeLayout时,参考组件用的也是id!

layout_width组件的宽度,一般写:**wrap_content**或者**match_parent(fill_parent)**,前者是控件显示的内容多大,控件就多大,而后者会填满该控件所在的父容器;当然也可以设置成特定的大小,比如我这里为了显示效果,设置成了200dp。

layout_height组件的宽度,内容同上。

gravity设置控件中内容的对齐方向,TextView中是文字,ImageView中是图片等等。

text设置显示的文本内容,一般我们是把字符串写到string.xml文件中,然后通过@String/xxx取得对应的字符串内容的,这里为了方便我直接就写到""里,不建议这样写!!!

textColor设置字体颜色,同上,通过colors.xml资源来引用,别直接这样写!

textStyle:设置字体风格,三个可选值:**normal**(无效果),**bold**(加粗),**italic**(斜体)

textSize:字体大小,单位一般是用sp!

background:控件的背景颜色,可以理解为填充整个控件的颜色,可以是图片哦!

backgrounTint:设置背景颜色

需求

1、文字过长时,我们需要在结尾使用“...”

<TextView
        android:id="@+id/tv_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="天哥在奔跑天哥在奔跑天哥在奔跑天哥在奔跑天哥在奔跑天哥在奔跑天哥在奔跑"
        android:textColor="#000000"
        android:maxLines="2"
        android:ellipsize="end"
        android:textSize="24sp" />

maxLines 是设置文本最大的行数,ellipsize 是这是“...”显示的位置,这里 end 表示在文末,也可以设置在前面或者中间。

.

2、在文字底部加下划线

方法1:

textView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);

方法2:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            textView.setText(Html.fromHtml("<u>忘记密码?</u>", Html.FROM_HTML_MODE_LEGACY));
        } else {
            textView.setText(Html.fromHtml("<u>忘记密码?</u>"));
        }

.

3、在文字旁加icon

android:drawableTop="@drawable/icon_user"
android:drawableRight="@drawable/icon_user"
android:drawableLeft="@drawable/icon_user"
android:drawableBottom="@drawable/icon_user"

icon_存放在项目目录 src/res/drawable-xxhdpi 下面,没有这个文件夹的化可以自己新建一个。

.

4、给文字添加背景,圆角的

首先在 src/res/drawable 目录下新建一个 xml 文件,使用 shape 画出圆角背景:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="1dp"
        android:color="#ffffff"/>

    <corners
        android:radius="5dp"/>

    <solid
        android:color="#226DDD"/>
</shape>
属性说明
stroke描边
corners圆角
solid填充

然后给 TextView 设置背景 background 属性,这里我们同时还设置了内边距 padding 属性:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    android:background="#000000">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="天哥在奔跑"
        android:background="@drawable/bg_round_blue"
        android:padding="5dp"
        android:textColor="#ffffff"
        android:textSize="20sp" />

</LinearLayout>

我们把布局的背景颜色设置成了黑色,这样我们能看到文字的背景是有白色描边的,是圆角的,是填充蓝色的。如果你不需要描边的话,可以不设置 stroke ,同理,solid 也可以不用设置,根据自己的需要来设置。圆角也可以只设置一个或几个,例如:android:topLeftRadius="5dp",设置左上为圆角。

.

5、TextView 加载HTML

textView.setText(Html.fromHtml("<html>...</html>"));

.

6、跑马灯效果

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:padding="5dp"
        android:singleLine="true"
        android:text="大家好,我是天哥,这是演示的TextView跑马灯效果"
        android:textColor="#000000"
        android:textSize="20sp" />

</LinearLayout>

需要注意的是,singleLine 表示单行显示,这个属性已经被废弃,写出来会有中划线,代替它的是 maxLines ,但是这里千万不要使用 maxLines 来代替,不然没有效果,或许这就是自身的 bug 吧。

引用:《Android 常用控件》TextView


谢谢你能看完呀~~~