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>
相关文章
|
开发工具 Android开发 iOS开发
如何在Android Studio中配置Flutter环境?
如何在Android Studio中配置Flutter环境?
3388 160
|
Android开发 数据安全/隐私保护 开发者
Android自定义view之模仿登录界面文本输入框(华为云APP)
本文介绍了一款自定义输入框的实现,包含静态效果、hint值浮动动画及功能扩展。通过组合多个控件完成界面布局,使用TranslateAnimation与AlphaAnimation实现hint文字上下浮动效果,支持密码加密解密显示、去除键盘回车空格输入、光标定位等功能。代码基于Android平台,提供完整源码与attrs配置,方便复用与定制。希望对开发者有所帮助。
385 0
|
Android开发 Windows
Android studio 报错Connect to 127.0.0.1:8888 [/127.0.0.1] failed: Connection refused: connect(已解决)
这是一篇关于解决Android Studio报错“Connect to 127.0.0.1:8888 failed: Connection refused”的文章。问题通常因系统代理设置被Android Studio自动保存导致。解决方法是找到系统中Android Studio使用的gradle.properties文件(位于Windows的C:\Users\你的电脑用户名\.gradle或Mac的/Users/.{你的用户目录}/.gradle),删除或注释掉多余的代理配置后保存并重新Sync项目。希望此经验能帮助快速解决同类问题!
3366 36
|
Java Android开发
Android studio中build.gradle文件简单介绍
本文解析了Android项目中build.gradle文件的作用,包括jcenter仓库配置、模块类型定义、包名设置及依赖管理,涵盖本地、库和远程依赖的区别。
1293 19
|
Dart 前端开发 Android开发
【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
774 4
【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
|
API 开发工具 Android开发
Android源码下载
Android源码下载
2851 0
|
存储 Ubuntu Linux
linphone android sdk 源码下载编译
linphone android sdk 源码下载编译
2549 0
linphone android sdk 源码下载编译
|
Ubuntu Java Linux
Android13源码下载及全编译流程
Android13源码下载及全编译流程
Android13源码下载及全编译流程
|
Android开发
【Binder 机制】分析 Android 内核源码中的 Binder 驱动源码 binder.c ( googlesource 中的 Android 内核源码 | 内核源码下载 )(一)
【Binder 机制】分析 Android 内核源码中的 Binder 驱动源码 binder.c ( googlesource 中的 Android 内核源码 | 内核源码下载 )(一)
660 0
【Binder 机制】分析 Android 内核源码中的 Binder 驱动源码 binder.c ( googlesource 中的 Android 内核源码 | 内核源码下载 )(一)
|
Android开发 C++
【Android 安装包优化】p7zip 源码交叉编译 Android 平台可执行程序 ( 下载 p7zip 源码 | 交叉编译 Android 中使用 7z 可执行程序 )(二)
【Android 安装包优化】p7zip 源码交叉编译 Android 平台可执行程序 ( 下载 p7zip 源码 | 交叉编译 Android 中使用 7z 可执行程序 )(二)
545 0
【Android 安装包优化】p7zip 源码交叉编译 Android 平台可执行程序 ( 下载 p7zip 源码 | 交叉编译 Android 中使用 7z 可执行程序 )(二)

热门文章

最新文章