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

news/2025/3/29 19:36:57/

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

一、设置文本的内容

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

 <TextViewandroid: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 {@Overrideprotected 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"><TextViewandroid:id="@+id/tv_px"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="你好,世界(px大小)"android:textSize="30px" /><TextViewandroid:id="@+id/tv_dp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="你好,世界(dp大小)"android:textSize="30dp" /><TextViewandroid: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"><TextViewandroid:id="@+id/tv_xml"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="布局文件设置六位文字颜色"android:textColor="#00ff00"android:textSize="17sp" /><TextViewandroid:id="@+id/tv_values"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="资源文件引用六位文字颜色"android:textColor="@color/green"android:textSize="17sp" /><TextViewandroid:id="@+id/tv_code_system"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="代码设置系统自带的颜色"android:textSize="17sp" /><TextViewandroid:id="@+id/tv_code_six"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="代码设置六位文字颜色"android:textSize="17sp" /><TextViewandroid:id="@+id/tv_code_eight"=android:textSize="17sp" /><TextViewandroid: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 {@Overrideprotected 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>


http://www.ppmy.cn/news/310763.html

相关文章

Compose (9/N) - 主题 Theme

一、Material Design 直接把任何Composable函数用 MaterialTheme{ } 包裹起来&#xff0c;就可以使用相关属性了。也可以单独将某个属性拿出来使用。 1.1 颜色 Color primary 主色&#xff0c;屏幕和元素都用这个颜色。 primaryVariant 用于区分主色&#xff0c;比如app bar和…

android 登录注册动画,Android开发(14)——动画实战:炫酷登录

本节内容 1.第三方库实现虚化 2.添加输入框和按钮 3.按钮状态 4.键盘隐藏 5.监听焦点改变的事件 6.手臂旋转动画 7.手掌和手臂动画 Demo简介 1.做一个炫酷登录的界面。 image.png 当我们输入密码的时候&#xff0c;猫头鹰会捂住眼睛。点击其他地方后&#xff0c;它的手臂又会张…

Android开发帮助技巧(适用于入门)二

Android配置配置清单&#xff0c;Gradle构建文件&#xff0c;外部化资源部分。 &#xff08;一)配置清单AndroidManifest.xml use-permission和uses-feature和application属于同等级节点。 android:allowBackup 是否允许应用参与备份和恢复基础架构。如果将此属性设为 fals…

【php + MySQL + Android】Android登陆界面设计(1)

前言 自己要做一个Android App&#xff0c;涉及到服务器端的用户数据管理&#xff0c;而网络上的的资源往往是只讲一个方面。所以自己每完成一段工作&#xff0c;会将开发过程记录下来&#xff0c;并供大家参考&#xff0c;相关文章放在专栏 【php MySQL Android】。 如果…

Jetpack Compose 初体验(上),retrofit原理面试

声明 Typography 对象&#xff0c;然后给 Text 添加 style 属性&#xff0c;来控制文字的样式。 Preview(showBackground true) Composable fun VerticalText() { val typography MaterialTheme.typography Column( modifier Modifier.padding(16.dp) ) { Image( painter …

scnu校赛去年题

求两个数的公约数有多少个 #include<iostream> #include<bits/stdc.h> using namespace std; int gcd(int a,int b){return b?gcd(b,a%b):a; } int main(){int T,a,b;cin>>T;while(T--){int ans1;cin>>a>>b;int ggcd(a,b);if(g1){cout<<…

STM32F10xx串口通信

一、通信方式相关 1.1 并行通信 1.2 串行通信 串行通信的通信方式&#xff1a; 常见的串行通信接口&#xff1a; STM32的串口通信接口 USART&#xff1a;通用异步收发器UART&#xff1a;通用同步异步收发器STM32F10x大容量系列芯片&#xff0c;包含3个USART&#xff08;支持异…

Android Compose Bloom 项目实战 (一) : 项目说明与配置

1. 项目介绍 Bloom是谷歌 AndroidDevChallenge (Android 开发挑战赛) 中的一期活动&#xff0c;目的是为了推广Compose&#xff0c;非常适合用来练手&#xff0c;通过这个项目&#xff0c;我们可以很好的入门Compose。本文介绍了如何从零开始&#xff0c;开发这个Compose项目。…