一手遮天 Android - view(文本类): TextView 常用属性

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

一手遮天 Android - view(文本类): TextView 常用属性

示例如下:

/view/text/TextViewDemo1.java

/**
 * TextView - 文本显示控件
 *
 * 演示 TextView 的常用属性的使用
 */

package com.webabcd.androiddemo.view.text;

import android.graphics.Color;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.TextView;

import com.webabcd.androiddemo.R;

public class TextViewDemo1 extends AppCompatActivity {

    private TextView _textView5;

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

        _textView5 = (TextView) findViewById(R.id.textView5);

        sample();
    }

    // 在 java 中设置 TextView 的常用属性
    private void sample() {
        _textView5.setTextColor(Color.BLUE);
        _textView5.setGravity(Gravity.CENTER);
        _textView5.setMaxLines(3);
        _textView5.setEllipsize(TextUtils.TruncateAt.END);
        /**
         * setText(CharSequence text) - 指定显示的文本
         * setText(int resid) - 指定显示的资源(会自动将资源转换为文本)
         */
        _textView5.setText(R.string.text_long);
        /**
         * TypedValue.COMPLEX_UNIT_PX - px
         * TypedValue.COMPLEX_UNIT_DIP - dp
         * TypedValue.COMPLEX_UNIT_SP - sp
         */
        _textView5.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
    }
}

/layout/activity_view_text_textviewdemo1.xml

<?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">

    <!--演示 TextView 的常用属性的使用-->

    <!--
        text - 需要显示的文本内容
        textColor - 文本的颜色
        background - 文本的背景颜色
        width, minWidth, maxWidth - 宽度(当 layout_width="wrap_content" 时有效)
        ems, minEms, maxEms - 以字符为单位的宽度,比如 ems="10" 代表宽度为 10 字符的宽度(当 layout_width="wrap_content" 时有效)
        height, minHeight, maxHeight - 高度(当 layout_height="wrap_content" 时有效)
        lines, minLines, maxLines - 行数(无论 layout_height 是什么值都有效)
        gravity - 对齐方式
            left, top, right, bottom, center
            注:支持“|”运算符
        maxLength - 限制显示文本的最大长度,超出部分不显示
    -->
    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#00F"
        android:background="#F00"
        android:textSize="32dp"
        android:lines="2"
        android:gravity="right|bottom"
        android:text="@string/text_short" />

    <!--
        ellipsize - 内容溢出时的显示方式
            none - 截断
            start - 开头部分用“...”表示
            end - 结尾部分用“...”表示
            middle - 中间部分用“...”表示(仅单行模式支持)
            marquee - 跑马灯效果,后面有详细介绍(仅单行模式支持)
        singleLine - 是否是单行显示(默认值是 false)
            这个属性过时了,但是如果只显示一行,并且指定了 ellipsize 属性的话,则建议用 singleLine="true" 替换 lines="1",否则在某些系统上会崩溃
    -->
    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:singleLine="true"
        android:ellipsize="middle"
        android:text="@string/text_long" />

    <!--
        ellipsize="marquee" - 跑马灯效果,不支持调整速度(仅单行模式支持)
            要想跑马灯,则 TextView 必须是焦点,所以还需要设置 focusable="true" 和 focusableInTouchMode="true"
        marqueeRepeatLimit - 重复次数,如果需要无限循环的话则设置为 -1 或 marquee_forever
    -->
    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:text="@string/text_long" />

    <!--
        要想跑马灯,则 TextView 必须是焦点,如果要想两个 TextView 都跑马灯呢?那就自定义一个 TextView 告诉系统我也是焦点
    -->
    <com.webabcd.androiddemo.view.text.TextViewMarquee
        android:id="@+id/textView4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="-1"
        android:text="@string/text_long" />

    <!--
        在 java 中设置 TextView 的常用属性
    -->
    <TextView
        android:id="@+id/textView5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" />

    <!--
        includeFontPadding - 是否包含顶部和底部的留白,默认值 true(上下各留白 2px,左右没有留白)
        textScaleX - 字符之间的间隔,默认值 1.0f
        lineSpacingExtra - 行间距,允许负数
        lineSpacingMultiplier - 行间距的倍数
    -->
    <TextView
        android:id="@+id/textView6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:lines="3"
        android:includeFontPadding="false"
        android:textScaleX="1.2"
        android:lineSpacingExtra="2dp"
        android:lineSpacingMultiplier="1.5"
        android:text="@string/text_long" />

    <!--
        autoLink - 是否需要自动识别电话/邮箱/链接,并将文本显示为可点击的链接且具有相应的行为
            none - 不识别
            phone - 识别电话,在 java 中为 setAutoLinkMask(Linkify.PHONE_NUMBERS)
            email - 识别邮箱,在 java 中为 setAutoLinkMask(Linkify.EMAIL_ADDRESSES)
            web - 识别链接,在 java 中为 setAutoLinkMask(Linkify.WEB_URLS)
            all - 识别电话/邮箱/链接,在 java 中为 setAutoLinkMask(Linkify.ALL)
    -->
    <TextView
        android:id="@+id/textView7"
        android:autoLink="all"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我的电话是 13911111111,我的邮箱是 email@hotmail.com,我的博客是 http://webabcd.cnblogs.com" />

</LinearLayout>

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

posted @ 2021-05-31 12:18  webabcd  阅读(172)  评论(0编辑  收藏  举报