运行有问题或需要源码请点赞关注收藏后评论区留言或私信博主
一、设置文本的内容
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>