本文将继续记录学习Android studio中的简单控件的文本颜色设置
文本颜色设计有以下几个方法:
(1)在Java代码中调用setTextColor方法即可设置文本颜色,具体色值可从Color类取。那么就需要我们在Java类中获取我们所需的颜色代码,并在xml中引用。
(2)在xml文件中通过android:textColor指定文本颜色,色值由透明度alpha和RGB三原色联合定义,色值有八位16进制和六位16进制两种表达方式,例如八位编码FFEEDDCC中,FF表示透明度,EE为红色的浓度,DD为绿色的浓度,CC为绿色的浓度。透明度FF表示完全不透明,00为完全透明。RGB三色数值越大,表示颜色越浓,也就越亮;数值越小,表示颜色越淡,也就越暗。
package com.example.chapter03;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
public class TestColorActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testcolor);
//从文本中获取名叫tv_code_system的文本视图
TextView tv_code_system = findViewById(R.id.tv_code_system);
//将tv_code_system的文本颜色设置为系统自带颜色
tv_code_system.setTextColor(Color.GREEN);
//从文本中获取名叫tv_code_eight的文本视图
TextView tv_code_eight = findViewById(R.id.tv_code_eight);
//将tv_code_eight的文本颜色设置为不透明的绿色,即正常的绿色
tv_code_eight.setTextColor(0xff00ff00);
//从布局文件中获取名叫tv_code_six的文本视图
TextView tv_code_six = findViewById(R.id.tv_code_six);
//将tv_code_six的文本颜色设置为透明的绿色即看不到的绿色
tv_code_six.setTextColor(0x00ff00);
//从文本中获取名叫tv_code_background的文本视图
TextView tv_code_background = findViewById(R.id.tv_code_background);
//颜色来自于系统自带元素
//tv_code_background.setBackgroundColor(Color.GREEN);
//颜色来自于资源文件
tv_code_background.setBackgroundResource(R.color.green);
}
}
在Java文件中设置好,系统自带的颜色,8位数字代表的颜色,16位代表的颜色,并使用资源文件的颜色对文本背景进行设置颜色,Java中的颜色设置完成之后,下面就是在xml文件中设置文本内容,并改变其颜色,上代码。
<?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_code_system"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="代码设置系统自带的颜色"
android:textSize="17sp"/>
<TextView
android:id="@+id/tv_code_eight"
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_xml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="代码设置六位文字颜色"
android:textSize="17sp"
android:textColor="#00ff00"/>
<TextView
android:id="@+id/tv_values"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="资源文件引入六位文字颜色"
android:textSize="17sp"
android:textColor="@color/green"/>
<TextView
android:id="@+id/tv_code_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="背景设置为绿色"
android:textSize="17sp"/>
</LinearLayout>
为了方便理解,我将方便明示代码通过什么方式来进行的颜色设置,我将文本内容设置为了对应的方法,且结合上一章的内容对文本的字体大小进行了设置。
运行结果如下
从运行结果上大家可以看到第二行和第四行文本中间有一行空白这是由于使用六位时将颜色设置为了透明的,第三行和第四行文本内容相同但是第四行文本利用的是xml中的textColor对文we色设置,最为特别的便是最后一行的文本,文字颜色为黑色但其背景为绿色,其背景大小为我们设置的17sp