使用颜色(color)资源

来源:互联网 发布:知乎员工数量 编辑:程序博客网 时间:2024/04/30 02:40

1.颜色值定义

颜色值的定义是通过RGB三原色和一个alpha值来定义的。颜色值定义的开始是一个#,后面是Alpha-Red-Green-Blue的格式。

如:#RGB    #ARGB    #RRGGBB    #AARRGGBB

2.下面通过一个实例来演示,设置红色背景和蓝色文字。蓝色字体在XML中设置,红色背景在代码中设置。

package com.lovo;import android.os.Bundle;import android.app.Activity;public class TestColorActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 设置布局setContentView(R.layout.activity_test_color);// 引用颜色资源,设置背景颜色为红色this.getWindow().setBackgroundDrawableResource(R.color.red_bg);}}


布局XML:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"><TextView android:text="测试颜色资源,红色背景,蓝色文字" android:id="@+id/TextView01"android:layout_width="wrap_content" android:layout_height="wrap_content"android:textColor="@color/blue_text" /></LinearLayout>


颜色XML:

<?xml version="1.0" encoding="utf-8"?><resources><!-- 前两位设置透明度 --><color name="red_bg">#ffff0000</color><!-- 相同的两位可以简写为一位,如下所示 --><color name="blue_text">#00f</color></resources>


 

原创粉丝点击