设置文本的颜色
各种设置文本颜色的方法
- 在Java中可以用以下几种方法设置
tv1.setTextColor(Color.RED);
// 使用Color类中的颜色
tv1.setTextColor(0xffff0000);
// 使用八位十进制颜色代码
tv1.setTextColor(0xff0000);
// 使用六位十进制颜色代码
- 在XML中可用以下几种方法设置
<TextView
android:id="@+id/tv1"
android:text="我是一个文本控件"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000"/>
<!--使用十六进制颜色代码-->
<TextView
android:id="@+id/tv2"
android:text="我是一个文本控件"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/Red"/>
<!--使用在values/colors.xml中定义的颜色-->
注意:使用XML的第二种方法,需要在'values/colors.xml'文件中定义Red
设置文本的背景颜色
各种设置文本背景颜色的方法
在Java中 这与设置文本颜色就相差了一个方法,把setTextColor换成setBackgroundColor就行了
tv1.setBackgroundColor(Color.RED);
// 使用Color类中的颜色
tv1.setBackgroundColor(0xffff0000);
// 使用八位十进制颜色代码
tv1.setBackgroundColor(0xff0000);
// 使用六位十进制颜色代码
在XML中
<TextView
android:id="@+id/tv1"
android:text="我是一个文本控件"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"/>
<!--使用十六进制颜色代码-->
<TextView
android:id="@+id/tv2"
android:text="我是一个文本控件"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/Red"/>
<!--使用在values/colors.xml中定义的颜色-->