Android Studio入门之文本内容、大小、颜色的讲解及实战(附源码 超详细必看)

简介: Android Studio入门之文本内容、大小、颜色的讲解及实战(附源码 超详细必看)

运行有问题或需要源码请点赞关注收藏后评论区留言或私信博主

一、设置文本的内容

1:在XML文件中通过属性android:text设置文本

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

2:在Java代码中调用文本视图对象的setText方法设置文本

TextView tv_hello=findViewById(R.id.tv_hello);
tv_hello.setText("玫瑰少年 在我心里");

3:在strings.xml中设置文本(强力推荐)

值得一提的是,如果使用上面两种方法,那么在我们想改变文本中的内容的时候就会非常麻烦,要一个一个XML文件或者Java代码去改,Android Studio推荐把字符串放到专门的地方管理,也就是res/values目录下的strings.xml文件

<resources>
    <string name="app_name">chapter03</string>
    <string name="hello">玫瑰少年,在我心里</string>
</resources>

修改完之后我们只需要回到XML布局文件,将android:text属性值改为@string/字符串名即可,相当于上指向strings.xml中的字符串。

至此不管是XML文件还是Java代码都从strings.xml引用字符串资源,所以以后只需要改动strings.xml一个地方即可(耦合度低,可维护性高)

设置文本的大小

TextView允许设置文本内容,也允许设置文本大小,在Java代码中调用setTextSize方法,即可指定文本大小。常见的字号单位主要有px,dp,sp三种 以下进行简要的介绍

1:px

px是手机屏幕的最小显示单位,它于设备的显示屏有关,一般来说,同样尺寸的屏幕,如果看起来越清晰,则表示像素密度越高

2:dp

指的是与设备无关的显示单位,它只与屏幕的尺寸有关

3:sp

sp原理跟dp差不多,但它专门用来设置字体大小。

下面通过一个实例讲解 效果如下

TextSizeActivity类代码如下

package com.example.chapter03;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class TextSizeActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_size);
        // 从布局文件中获取名叫tv_sp的文本视图
        TextView tv_sp = findViewById(R.id.tv_sp);
        tv_sp.setTextSize(30); // 设置tv_sp的文本大小
    }
}

activity_text_size.xml文件代码如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv_px"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你好,世界(px大小)"
        android:textSize="30px" />
    <TextView
        android:id="@+id/tv_dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你好,世界(dp大小)"
        android:textSize="30dp" />
    <TextView
        android:id="@+id/tv_sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你好,世界(sp大小)"
        android:textSize="30sp" />
</LinearLayout>

设置文本的颜色

除了设置文字大小 文字颜色也需要经常修改,毕竟Android默认的灰色文字不够醒目,在Java代码中调用setTextColor方法即可设置文本颜色 颜色属性值如下

可是XML文件无法引用Color的颜色常量,为此Android制定了一套规范的编码标准,将色值交由透明度alpha,RGB三原色(红绿蓝三种颜色)联合定义。该标准又有八位十六进制数与六位十六进制数两种表达方式。 效果图如下

activity_text_color.xml代码如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv_xml"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="布局文件设置六位文字颜色"
        android:textColor="#00ff00"
        android:textSize="17sp" />
    <TextView
        android:id="@+id/tv_values"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="资源文件引用六位文字颜色"
        android:textColor="@color/green"
        android:textSize="17sp" />
    <TextView
        android:id="@+id/tv_code_system"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="代码设置系统自带的颜色"
        android:textSize="17sp" />
    <TextView
        android:id="@+id/tv_code_six"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="代码设置六位文字颜色"
        android:textSize="17sp" />
    <TextView
        android:id="@+id/tv_code_eight"
   =
        android:textSize="17sp" />
    <TextView
        android:id="@+id/tv_code_background"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="背景设置为绿色"
        android:textSize="17sp"
        android:background="#00ff00"/>
=

TextColorActivity类代码如下

package com.example.chapter03;
import android.graphics.Color;
import androidx.appcompat.app.AppCompatActivity;
public class TextColorActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView tv_code_system = findViewById(R.id.tv_code_system);
        // 将tv_code_system的文字颜色设置系统自带的绿色
        tv_code_system.setTextColor(Color.GREEN);
        // 从布局文件中获取名叫tv_code_six的文本视图
        TextView tv_code_six = findViewById(R.id.tv_code_six);
        // 将tv_code_six的文字颜色设置为透明的绿色,透明就是看不到
        tv_code_six.setTextColor(0x00ff00);
        // 从布局文件中获取名叫tv_code_eight的文本视图
        TextView tv_code_eight = findViewById(R.id.tv_code_eight);
        // 将tv_code_eight的文字颜色设置为不透明的绿色,即正常的绿色
        tv_code_eight.setTextColor(0xff00ff00);
        // 从布局文件中获取名叫tv_code_background的文本视图
        TextView tv_code_background = findViewById(R.id.tv_code_background);
        // 将tv_code_background的背景颜色设置为绿色
        tv_code_background.setBackgroundColor(Color.GREEN); // 在代码中定义的色值
        tv_code_background.setBackgroundResource(R.color.green); // 颜色来源于资源文件
    }
}

当然还需要把color.xml文件中改为以下代码添加依赖

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="purple_200">#FFBB86FC</color>
    <color name="purple_500">#FF6200EE</color>
    <color name="purple_700">#FF3700B3</color>
    <color name="teal_200">#FF03DAC5</color>
    <color name="teal_700">#FF018786</color>
    <color name="black">#FF000000</color>
        <color name="colorPrimary">#008577</color>
        <color name="colorPrimaryDark">#00574B</color>
        <color name="colorAccent">#D81B60</color>
        <color name="red">#FF0000</color>
        <color name="green">#4CAF50</color>
        <color name="white">#FFFFFF</color>
</resources>
相关文章
|
8天前
|
JSON 编译器 开发工具
VS Code阅读Android源码
VS Code阅读Android源码
12 1
|
7天前
|
Java Android开发
Android系统 修改无源码普通应用为默认Launcher和隐藏Settings中应用信息图标
Android系统 修改无源码普通应用为默认Launcher和隐藏Settings中应用信息图标
22 0
|
1天前
|
移动开发 API Android开发
Android应用性能优化实战
【4月更文挑战第28天】在移动开发领域,一个流畅的用户体验是至关重要的。对于Android开发者而言,应用的性能优化是一项既挑战性也极其重要的工作。本文将深入探讨Android应用性能优化的多个方面,包括内存管理、UI渲染、多线程处理以及电池效率等,旨在为开发者提供实用的性能提升策略和具体的实施步骤。通过分析常见的性能瓶颈,并结合最新的Android系统特性和工具,我们的目标是帮助读者打造更加高效、响应迅速的Android应用。
|
3天前
|
缓存 监控 Android开发
Android 应用性能优化实战
【4月更文挑战第27天】 在竞争激烈的移动应用市场中,性能优越的应用更能吸引和保留用户。针对Android平台,本文将深入探讨影响应用性能的关键因素,并提供一系列实用的优化策略。我们将从内存管理、UI渲染、多线程处理以及电池使用效率等方面入手,通过具体案例分析如何诊断常见问题,并给出相应的解决方案。文中所提技巧旨在帮助开发者构建更加流畅、高效的Android应用。
15 2
|
5天前
|
缓存 移动开发 Android开发
构建高效Android应用:内存优化实战指南
【4月更文挑战第25天】 在移动开发领域,应用程序的性能至关重要。特别是对于Android设备,由于硬件配置的多样性,合理的内存管理与优化是提升应用流畅度、减少卡顿和崩溃的关键。本文将深入探讨Android应用的内存优化技巧,通过分析内存泄漏的原因、诊断工具的运用以及实际代码层面的改进措施,帮助开发者构建更加高效的Android应用。
|
7天前
|
Linux 开发工具 Android开发
Docker系列(1)安装Linux系统编译Android源码
Docker系列(1)安装Linux系统编译Android源码
13 0
|
7天前
|
安全 Java Shell
Android13 adb input 调试命令使用和源码解析
Android13 adb input 调试命令使用和源码解析
12 0
|
8天前
|
传感器 Java 开发工具
[NDK/JNI系列03] Android Studio集成NDK开发环境
[NDK/JNI系列03] Android Studio集成NDK开发环境
13 0
|
14天前
|
数据采集 小程序 数据可视化
Java Android原生智慧校园管理系统源码
对班牌的考试模式、班牌模式上课模式进行设置及管理,设置成功后,班牌端将同步应用。
21 0
|
前端开发 程序员 开发工具