一、设置文本的内容
1.方式:
(1)在XML文件中通过属性:android:text设置文本
例如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv_hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你好,世界"
        />
</LinearLayout>
在XML文件中设置文本时,将鼠标移动到“你好,世界”上方时,会弹出如下提示框:
该提示内容意思是这几个字是硬编码的字符串,建议使用来自@string的资源
所以我们将字符串放在res/values目录下的strings.xml文件中

此时strings.xml文件中已经定义了一个名为“app_name”的字符串常量,其值为“chapter01”
我们在此添加一个新的字 符串定义,字符串名为“hello”,字符串值为“你好,世界”

回到XML布局文件,我们将android:text属性值改为“@string/字符串名”

(2)在Java代码中调用文本视图对象的setText方法设置文本
例如:
package com.example.chapter01;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class ViewBorderActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_border);
        // 获取名为tv_hello的文本视图
        TextView tv_hello = findViewById(R.id.tv_hello);
        // 通过setText方法设置文字内容
        tv_hello.setText("你好,世界");
    }
}
在Java文件中引用字符串资源,则调用setText方法时填写“R.string.字符串名”

二、设置文本的大小
1.方式:
(1)在Java代码中调用setTextSize方法
例如:
package com.example.chapter01;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
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的文本大小
        tv_sp.setTextSize(30);
    }
}
这里数值越大,看到的文本就越大,数值越小,看到的文本就越小
(2)在XML文件中通过android:textSize指定文本大小
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv_sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:textSize="30sp"
        />
</LinearLayout>
文本大小存在不同的字号单位,XML文件中要求在字号数字后面写明单位类型,常见的字号单位主要有px,dp,sp三种
① px
px是手机屏幕的最小显示单位,它与设备的显示屏有关。一般来说,同样尺寸的屏幕(比如6英寸手 机),如果看起来越清晰,则表示像素密度越高,以px计量的分辨率也越大。
② dp
dp有时也写作dip,指的是与设备无关的显示单位,它只与屏幕的尺寸有关。一般来说,同样尺寸的屏幕以dp计量的分辨率是相同的,比如同样是6英寸手机,无论它由哪个厂家生产,其分辨率算成dp单位都是一个大小。
③ sp
sp的原理跟dp差不多,但它专门用来设置字体大小。手机在系统设置里可以调整字体的大小(小、标 准、大、超大)。设置普通字体时,同数值dp和sp的文字看起来一样大;如果设置为大字体,用dp设置 的文字没有变化,用sp设置的文字就变大了。 字体大小采用不同单位的话,显示的文字大小各不相同。例如,30px、30dp、30sp这3个字号,在不同手机上的显示大小有所差异。有的手机像素密度较低,一个dp相当于两个px,此时30px等同于15dp; 有的手机像素密度较高,一个dp相当于3个px,此时30px等同于10dp。假设某个App的内部文本使用字 号30px,则该App安装到前一部手机的字体大小为15dp,安装到后一部手机的字体大小为10dp,显然 后一部手机显示的文本会更小。

三、设置文本颜色
在Java代码中调用setTextColor方法设置文本颜色

例如:

package com.example.chapter01;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
public class TextColorActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_color);
        //从布局文件中获取名叫tv_code_system的文本视图
        TextView tv_code_system = findViewById(R.id.tv_code_system);
        //将tv_code_system的文字颜色设置为系统自带的绿色
        tv_code_system.setTextColor(Color.GREEN);
    }
}

package com.example.chapter01;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
public class TextColorActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_color);
        //从布局文件中获取名叫tv_code_system的文本视图
        TextView tv_code_system = findViewById(R.id.tv_code_system);
        //将tv_code_system的文字颜色设置为系统自带的绿色
        tv_code_system.setTextColor(Color.GREEN);
        //从布局文件中获取名叫tv_color_six的文本视图
        TextView tv_color_six = findViewById(R.id.tv_code_six);
        //将tv_color_six的文字颜色设置为透明的绿色
        tv_color_six.setTextColor(0x00ff00);
    }
}
此时我们看不到tv_code_six控件的文本,因为它是透明的
(2)八位色值

package com.example.chapter01;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
public class TextColorActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_color);
        //从布局文件中获取名叫tv_code_system的文本视图
        TextView tv_code_system = findViewById(R.id.tv_code_system);
        //将tv_code_system的文字颜色设置为系统自带的绿色
        tv_code_system.setTextColor(Color.GREEN);
        //从布局文件中获取名叫tv_color_six的文本视图
        TextView tv_color_six = findViewById(R.id.tv_code_six);
        //将tv_color_six的文字颜色设置为透明的绿色
        tv_color_six.setTextColor(0x00ff00);
        //从布局文件中获取名叫tv_color_eight的文本视图
        TextView tv_color_eight = findViewById(R.id.tv_code_eight);
        //将tv_color_eight的文字颜色设置为不透明的绿色,即正常的绿色
        tv_color_eight.setTextColor(0xff00ff00);
    }
}在XML文件中可以通过属性android:textColor设置文字颜色,但要给色值添加井号前缀“#”,设定好文本颜色的TextView标签示例如下:

<TextView
        android:id="@+id/tv_xml"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="布局文件设置六位文字颜色"
        android:textSize="17sp"
        android:textColor="#00ff00"
        />和字符串资源一样,Android把颜色也当作一种资源,在res/values目录下的colors.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="white">#FFFFFFFF</color>
    <color name="green">#00ff00</color>
</resources>然后回到XML布局文件中,把android:textColor属性值改为“@color/颜色名称”

<TextView
        android:id="@+id/tv_values"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="资源文件引用六位文字颜色"
        android:textSize="17sp"
        android:textColor="@color/green"
        /> 
 
<TextView
        android:id="@+id/tv_code_background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="背景设置为绿色"
        android:textSize="17sp"
        />例如:

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


















