微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在android开发中怎么使用颜色资源

这篇文章主要讲解了“在android开发中怎么使用颜色资源”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“在android开发中怎么使用颜色资源”吧!

定义一个名称为“Chapter03_Resource”的 Android 工程,在该工程的 resvalues 目录下,定义一个 colors.xml 颜色资源文件内容如下所示。 
       <?xml version="1.0" encoding="utf-8"?> 
       <resources> 
               <color name="red_bg">#f00</color>
               <color name="blue_text">#0000ff</color>
        </resources> 
在该工程的 reslayout目录下定义一个布局资源文件,在该文件添加一个 TextView 视图组件,引用颜色资源,设置视图组件 TextView 的文字颜色为蓝色。 
<?xml version="1.0" encoding="utf-8"?>
       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
              android:orientation="vertical"
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent">
        <TextView android:text="测试颜色资源,红色背景,蓝色文字
              android:id="@+id/TextView01"
               android:layout_width="wrap_content" 
              android:layout_height="wrap_content" 
              android:textColor="@color/blue_text"
       />
        </LinearLayout> 
定义一个 TestColorActivity 类,引用颜色资源文件,设置背景色为红色。 
package com.amaker.ch03.color;
      import android.app.Activity;
       import android.os.Bundle; 
       import com.amaker.test.R; 
public class TestColorActivity extends Activity {
       @Override 
              public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState); 
              setContentView(R.layout.test_color); 
              // 引用颜色资源,设置背景色为红色
               getwindow().setBackgroundDrawableResource(R.color.red_bg);
               }
        } 

感谢各位的阅读,以上就是“在android开发中怎么使用颜色资源”的内容了,经过本文的学习后,相信大家对在android开发中怎么使用颜色资源这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程之家,小编将为大家推送更多相关知识点的文章,欢迎关注!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐