Android UI 组件系列(一):TextView 使用详解与常见属性

server/2025/3/17 3:19:31/

引言

在 Android 开发中,TextView 是最常用的 UI 组件之一,它负责显示文本内容。无论是用于显示静态文本、动态内容,还是作为交互元素,TextView 都是构建用户界面不可或缺的一部分。随着应用的复杂性增加,TextView 不仅仅局限于简单的文本显示,它还提供了丰富的属性和功能,可以帮助开发者实现更灵活、更个性化的界面设计。

本文将详细介绍 TextView 的使用方法和常见属性,涵盖从基础的文本设置到高级的自定义功能,帮助你更好地掌握这个强大的 UI 组件。我们还会探讨一些常见的应用场景,以及如何通过 TextView 提供的功能实现更具交互性的用户体验。

TextView 的基本使用

接下来,我们将通过 XML 在 LinearLayout 容器中创建一个简单的 TextView,并在 Java 代码中动态修改它的文本内容。通过这个示例,介绍 TextView 在布局文件中的基本用法,以及如何在代码中进行动态更新。

在布局文件中创建 TextView

首先,我们需要在布局文件中定义一个 TextView。这里我们使用 LinearLayout 作为父容器,将 TextView 嵌套其中。

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"android:gravity="center"android:id="@+id/main"android:padding="16dp"><!-- 创建一个 TextView 并设置其属性 --><TextViewandroid:id="@+id/myTextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello, Android!"android:textSize="20sp"android:textColor="#000000" />
</LinearLayout>

在TextView组件中:

  1. 使用android:text 属性设置了TextView显示文本内容。
  2. 使用android:textSize属性设置了文本的字体大小。
  3. 使用android:textColor设置了文本颜色为黑色。

TextView 会自动根据文本内容调整宽高(因为使用了 wrap_content),并根据设置的文本样式进行渲染。

在 Java 代码中动态修改 TextView

除了在 XML 中定义 TextView,我们还可以通过 Java 或 Kotlin 代码动态修改 TextView 的文本内容。这通常用于需要实时更新显示的场景,比如根据用户的操作或从网络加载的数据来更新界面。

例如,在 Java 中,我们可以通过以下方式来设置 TextView 的文本:

public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);EdgeToEdge.enable(this);setContentView(R.layout.activity_main);// 设置TextView的文字setTextViewText();ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);return insets;});}/// 设置TextView的文字private void  setTextViewText() {TextView myTextView = findViewById(R.id.myTextView);myTextView.setText("Welcome to Android Development!");}}

这段代码会将 TextView 的文本内容更改为“Welcome to Android Development!”

TextView 的常见属性

TextView 提供了丰富的属性,允许你精细控制文本的显示效果。下面我们将介绍一些常用的属性,帮助你实现更多自定义功能。

1. android:text 设置文本内容

android:text 这是设置 TextView 显示文本内容的最基本属性。你可以直接在 XML 中设置一个静态文本,或者引用字符串资源。

<TextViewandroid:id="@+id/myTextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello, Android!" />

或者使用字符串资源:

<TextViewandroid:id="@+id/myTextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hello_android" />

2. android:textSize 设置文本大小

android:textSize 设置文本的大小,单位通常使用 sp 可缩放像素,使用 sp 可以确保文本在不同的屏幕密度下具有一致的显示效果。

<TextViewandroid:id="@+id/myTextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello, Android!"android:textSize="20sp" />

3. android:textColor 设置文本颜色

android:textColor 设置文本颜色,可以使用颜色代码或引用颜色资源。你可以直接设置一个颜色值,或者引用颜色资源文件(如@color/black)。

<TextViewandroid:id="@+id/myTextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello, Android!"android:textColor="#FF0000" /> <!-- 红色 -->

或者引入颜色资源:

<TextViewandroid:id="@+id/myTextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello, Android!"android:textColor="@color/text_color" />

4. android:textStyle 设置文本样式

android:textStyle 设置文本的样式,常用的选项有 blod(加粗)、italic(斜体)或同时设置这两者。

<TextViewandroid:id="@+id/myTextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello, Android!"android:textStyle="bold|italic" />

5. android:gravity 设置文本对齐方式

android:gravity 控制文本在 TextView 内部的对齐方式。常用的对齐方式有:left、center、right、top、bottom,以及它们的组合(如center_vertical、center_horizontal)。

<TextViewandroid:id="@+id/myTextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello, Android!"android:gravity="center" /> <!-- 文本居中 -->

6. android:lineSpacingExtra、android:lineSpacingMultiplier 文本行间距

android:lineSpacingExtra 设置文本行间的额外增量,单位是像素(px)。

android:lineSpacingMultiplier 设置文本行间距的倍数,默认是1,表示不改变行间距。

<TextViewandroid:id="@+id/myTextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello, Android!"android:lineSpacingExtra="5dp"android:lineSpacingMultiplier="1.2" />

7. android:maxLines 设置最大行数

android:maxLines 限制文本显示的最大行数。

<TextViewandroid:id="@+id/myTextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="This is a long text that might overflow and get truncated"android:maxLines="2"/>

8. android:ellipsize 设置溢出处理

android:ellipsize 当文本内容超出显示区域时,设置如何处理。常用值有:end(省略号显示在文本末尾)、middle (省略号显示在中间)、start(省略号显示在文本开始部分)。

<TextViewandroid:id="@+id/myTextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="This is a long text that might overflow and get truncated"android:ellipsize="end" />

TextView 的高级特性

在日常开发中,除了常见的文本设置外,TextView 还提供了更强大的功能,能够满足更复杂的需求。接下来,我们将介绍一些 TextView 的高级特性,包括字体、文本效果以及如何在 TextView 中插入图片。

1. android:fontFamily 设置自定义字体

android:fontFamily 使用该属性可以设置 TextView 的字体样式。你可以选择系统字体,或者通过在 assets 文件夹中引入自定义字体。

例如,设置系统字体为 sans-serif

<TextViewandroid:id="@+id/myTextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello, Custom Font!"android:fontFamily="sans-serif" />

如果想要使用自定义字体,可以将字体文件放到assets/fonts目录下,然后通过代码来设置字体

Typeface customFont = Typeface.createFromAsset(getAssets(), "fonts/my_custom_font.ttf");
textView.setTypeface(customFont);

2. android:textAllCaps 设置文本是否全部大写

android:textAllCaps 此属性可用于设置文本是否全部显示大写字母。它的值可以是true 或 false。

<TextViewandroid:id="@+id/myTextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello, Android!"android:textAllCaps="true" />

3. 使用Spannable 设置富文本

如果想要在TextView中显示混合样式的文本(例如某部分文本加粗、着色或添加下划线),你可以使用 Spannable 类来处理。Spannable 提供了多种方式来实现文本的样式设置。

例如 使用 SpannableString设置部分文本为加粗和红色:

TextView textView = findViewById(R.id.myTextView);
SpannableString spannableString = new SpannableString("Hello, Android!");
spannableString.setSpan(new StyleSpan(Typeface.BOLD), 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);

4. TextView 设置图片

TextView 支持图片与文本一起显示。通过设置android:drawableLeft、android:drawableRight、android:drawableTop、android:drawableBottom属性,TextView可以在文本的四个方向上显示图片,来实现图文混排的效果。

<TextViewandroid:id="@+id/myTextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello, with image!"android:drawableLeft="@drawable/ic_example"android:drawablePadding="10dp" />

或者通过代码动态设置图片

TextView textView = findViewById(R.id.myTextView);
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_example, 0, 0, 0);

5. 设置文本阴影

android:textShadowColor 设置文本阴影的颜色。

android:textShadowDx 设置阴影在X轴上的偏移量。

android:textShadowDy 设置阴影在Y轴上的偏移量。

android:textShadowRadius 设置阴影的模糊半径。

通过这些属性,我们可以为文本添加阴影效果,提升界面的视觉效果。

<TextViewandroid:id="@+id/myTextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello, Shadow!"android:textShadowColor="#888888"android:textShadowDx="2dp"android:textShadowDy="2dp"android:textShadowRadius="5dp" />

结语

TextView 是 Android 开发中非常常用的 UI 组件,它不仅支持基本的文本显示,还提供了许多强大的功能和灵活的自定义选项。从简单的文本设置到图文混排、字体样式、阴影效果、富文本、HTML 渲染等,TextView 都能够满足大多数界面展示需求。掌握 TextView 的常见属性和高级特性,可以帮助开发者更加高效地设计和实现复杂的用户界面,使应用界面更加丰富和多样化。


http://www.ppmy.cn/server/175597.html

相关文章

蓝桥 2109统计子矩阵

问题描述 给定一个NM 的矩阵 A, 请你统计有多少个子矩阵 (最小 11, 最大 NM) 满足子矩阵中所有数的和不超过给定的整数 K ? 输入格式 第一行包含三个整数 N,M 和 K. 之后 N 行每行包含 M 个整数, 代表矩阵 A. 输出格式 一个整数代表答案。 样例输入 3 4 10 1 2 3 4 5 …

DeepSeek-R1深度解读

deepseek提出了一种通过强化学习&#xff08;RL&#xff09;激励大语言模型&#xff08;LLMs&#xff09;推理能力的方法&#xff0c;个人认为最让人兴奋的点是&#xff1a;通过RL发现了一个叫“Aha Moment”的现象&#xff0c;这个时刻发生在模型的中间版本中。在这个阶段&…

LeetCode --- 440周赛

题目列表 3477. 将水果放入篮子 II 3478. 选出和最大的 K 个元素 3479. 将水果装入篮子 III 3480. 删除一个冲突对后最大子数组数目 一、将水果放入篮子 II 本题由于数据范围比较小&#xff0c;所以我们可以暴力模拟水果的放置过程&#xff0c;代码如下 // C class Solution…

05延迟任务精准发布文章(redis实现延迟任务、分布式锁)

上架不代表发布(需要发布app端才会显示文章&#xff09; 1)文章定时发布 2)延迟任务概述 2.1)什么是延迟任务 定时任务&#xff1a;有固定周期的&#xff0c;有明确的触发时间 延迟队列&#xff1a;没有固定的开始时间&#xff0c;它常常是由一个事件触发的&#xff0c;而在…

新矩阵(信息学奥赛一本通-2041)

【题目描述】 已知一个nn(2≤n≤20)的矩阵&#xff08;方阵&#xff09;&#xff0c;把矩阵二条对角线上的元素值加上10&#xff0c;然后输出这个新矩阵。 【输入】 第一行为n; 下面为一个nn&#xff0c;矩阵中各正整数小于100。 【输出】 输出新的矩阵。共n行&#xff0c;每行…

聊聊langchain4j的AiServicesAutoConfig

序 本文主要研究一下langchain4j-spring-boot-starter的AiServicesAutoConfig LangChain4jAutoConfig dev/langchain4j/spring/LangChain4jAutoConfig.java AutoConfiguration Import({AiServicesAutoConfig.class,RagAutoConfig.class,AiServiceScannerProcessor.class })…

金融时间序列分析(Yahoo Finance API实战)

这里写目录标题 金融时间序列分析(Yahoo Finance API实战)1. 引言2. 项目背景与意义3. 数据集介绍4. GPU加速在数据处理中的应用5. 交互式GUI设计与加速处理6. 系统整体架构7. 数学公式与指标计算8. 完整代码实现9. 代码自查与BUG排查10. 总结与展望金融时间序列分析(Yahoo …

centos7通过yum安装redis

centos7通过yum安装redis 1.安装redis数据库 yum install -y redis2.启动redis服务 systemctl start redis3.查看redis状态 systemctl status redis4、停止服务 systemctl stop redis5、重启服务 systemctl restart redis6、查看redis进程 ps -ef | grep redis7、开放端…