Android开发中颜色的使用

Android 开发中使用的颜色可以分为两种,自定义颜色和系统颜色
1.自定义颜色:
    颜色值的定义是通过RGB三原色和一个alpha值来定义的(加色原理)。以井号(#)开始,后面是Alpha-Red-Green-Blue的格式。
形如:
#RGB
#ARGB
#RRGGBB
#AARRGGBB

通常使用#RRGGBB 或者#AARRGGBB的形式

1.1 在资源文件中定义颜色:
一般在res\values下建立colors.xml文件,定义颜色,如下:

<?xml version="1.0" encoding="utf-8"?>
<resourses>
    <color name="red">#ff0000</color>
</resourses>

1.2 颜色的使用
    

    1.2.1 在代码中使用颜色
R.color.color_name
例如:

Button btn1 = (Button) findViewById(R.id.button1);    
int color = Resources.getSystem().getColor(R.color.red);
btn1.setBackgroundColor(color);


    1.2.2 在布局文件中使用颜色
    @[package:]color/color_name
    例如:

<Button
    android:id="@+id/button1"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:text="Address book"
    android:background="@color/red"
></Button>

这个地方也可以直接使用颜色值,但是不推荐这样做


   
   
<Button
    android:id="@+id/button1"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:text="Address book"
    android:background="#ff0000"
></Button>

2.系统颜色
android也有一些内置的颜色,例如系统资源中定义的颜色,十分有限。
android.graphics.Color类中也提供了一些颜色常量和构造颜色值的静态方法。

    2.1 系统颜色的使用
        2.1.1 在代码中使用系统颜色

系统资源中定义的颜色值十分有限
Button btn1 = (Button) findViewById(R.id.button1);

int color = Resources.getSystem().getColor(android.R.color.background_dark);
btn1.setBackgroundColor(color);

Color类中的颜色常量
Button btn1 = (Button) findViewById(R.id.button1);    
btn1.setBackgroundColor(Color.CYAN);

使用Color类中的静态方法

Button btn1 = (Button) findViewById(R.id.button1);    
btn1.setBackgroundColor(Color.argb(0xff, 0xff, 0x00, 0x00));

     2.1.2 在布局文件中使用系统颜色
<Button
    android:id="@+id/button1"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:text="Address book"
    android:background="@android:color/background_dark"
></Button>


参考
http://developer.android.com/guide/topics/resources/more-resources.html
http://dev.10086.cn/cmdn/wiki/index.php?doc-view-6334.html



    
    


    
    


    
    


    
    


    
    


    
    


    
    


   
   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值