android 各种控件颜色值的设置(使用Drawable,Color)

来源:互联网 发布:reactrouter 源码详解 编辑:程序博客网 时间:2024/05/16 19:26
首先给出一个颜色值网站:http://www.114la.com/other/rgb.htm下面内容看到有个哥们写的挺好的,就直接转载了(转自:
http://blog.csdn.net/wangjia55/article/details/7815757


在Android中,如果需要改变控件默认的颜色,包括值的颜色,需要预先在strings.xml中设置,类似字符串,可以反复调用。Android中颜色可以使用drawable或是color来定义。
本例中strings.xml内容:

[java] view plaincopy
  1. <a href="http://www.pocketdigi.com/20110509/266.html" rel="bookmark" style="font-size: 14px; color: rgb(51, 68, 34); text-decoration: none; "><?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello">Hello World, Main!</string>  
  4.     <string name="app_name">Color</string>  
  5.     <drawable name="red">#ff0000</drawable>  
  6.     <color name="gray">#999999</color>  
  7.     <color name="blue">#0000ff</color>  
  8.     <color name="background">#ffffff</color>  
  9. </resources></a>  


上面定义了几个颜色值,下面是在布局文件中的调用,main.xml内容:

[java] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:background="@color/background"  
  7.     >  
  8. <TextView  android:id="@+id/tv1"  
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"   
  11.     android:text="@string/hello"  
  12.     android:textColor="@drawable/red"  
  13.     />  
  14. <TextView  android:id="@+id/tv2"  
  15.     android:layout_width="fill_parent"   
  16.     android:layout_height="wrap_content"   
  17.     android:text="@string/hello"  
  18.     android:textColor="@color/gray"  
  19.     />  
  20. <TextView  android:id="@+id/tv3"  
  21.     android:layout_width="fill_parent"   
  22.     android:layout_height="wrap_content"   
  23.     android:text="@string/hello"  
  24.     />  
  25. </LinearLayout>  


在Java程序中使用:

[java] view plaincopy
  1. package com.pocketdigi.color;  
  2.    
  3. import android.app.Activity;  
  4. import android.graphics.Color;  
  5. import android.os.Bundle;  
  6. import android.widget.TextView;  
  7.    
  8. public class Main extends Activity {  
  9.     /** Called when the activity is first created. */  
  10.     TextView tv1,tv2,tv3;  
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.main);  
  15.         tv1=(TextView)findViewById(R.id.tv1);  
  16.         tv2=(TextView)findViewById(R.id.tv2);  
  17.         tv3=(TextView)findViewById(R.id.tv3);  
  18.         tv3.setTextColor(Color.BLUE);//直接使用android.graphics.Color的静态变量  
  19.         tv2.setTextColor(this.getResources().getColor(R.color.blue));//使用预先设置的颜色值  
  20.    
  21.     }  
  22. }  

0 0
原创粉丝点击