温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2022-04-20 16:33:34 来源:亿速云 阅读:182 作者:iii 栏目:大数据

这篇文章主要讲解了“在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开发中怎么使用颜色资源这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI