zoukankan      html  css  js  c++  java
  • Android中颜色的使用

    开发中使用的颜色可以分为两种,自定义颜色和系统颜色

    1.自定义颜色:

    颜色值的定义是通过RGB三原色和一个alpha值来定义的(加色原理)。以井号(#)开始,后面是Alpha-Red-Green-Blue的格式。
    形如:
    #RGB 
    #ARGB 
    #RRGGBB 
    #AARRGGBB
    通常使用#RRGGBB 或者#AARRGGBB的形式。其中AA为透明度,FF表示不透明,00表示透明。

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

    <?xml version="1.0" encoding="utf-8"?>  

    <resourses>  

      <colorname="red">#ff0000</color>  

    </resourses>

    1.2 颜色的使用    

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

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


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

    1. <Button  
    2.     android:id="@+id/button1"  
    3.     android:layout_height="wrap_content"  
    4.     android:layout_width="match_parent"  
    5.     android:text="Address book"  
    6.     android:background="@color/red"  
    7. ></Button>  

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

     

    1. <Button  
    2.     android:id="@+id/button1"  
    3.     android:layout_height="wrap_content"  
    4.     android:layout_width="match_parent"  
    5.     android:text="Address book"  
    6.     android:background="#ff0000"  
    7. ></Button>  

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

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

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

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

    Color类中的颜色常量

    1. Button btn1 = (Button) findViewById(R.id.button1);      
    2. btn1.setBackgroundColor(Color.CYAN);  

    使用Color类中的静态方法

    1. Button btn1 = (Button) findViewById(R.id.button1);      
    2. btn1.setBackgroundColor(Color.argb(0xff0xff0x000x00));  

        2.1.2 在布局文件中使用系统颜色

      1. <Button  
      2.     android:id="@+id/button1"  
      3.     android:layout_height="wrap_content"  
      4.     android:layout_width="match_parent"  
      5.     android:text="Address book"  
      6.     android:background="@android:color/background_dark"  
      7. ></Button>  

    本文转自http://www.linuxidc.com/Linux/2011-10/45589.htm,感谢作者的分享

  • 相关阅读:
    SQL2005 SQL2008 远程连接配置方法
    Subvision 安装 部署 TortoiseSVN
    在wpf或winform关闭子窗口或对子窗口进行某个操作后刷新父窗口
    C# 中的委托和事件
    长数字隔三位用逗号","隔开,保留两位小数,指定长度,不足补空格
    C# 柱状图, 折线图, 扇形图
    如何在Visual Studio 2010旗舰版本下安装Window Phone 7 简体中文开发环境
    vs2010发布、打包安装程序(超全超详细)
    java 环境搭建
    SQL2008 转 2000(高版本转换到低版本)
  • 原文地址:https://www.cnblogs.com/yaksha/p/4186309.html
Copyright © 2011-2022 走看看