android 各种控件颜色值的设置(使用Drawable,Color)
2014-01-23 19:33
387 查看
首先给出一个颜色值网站: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
<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"?>
<resources>
<string name="hello">Hello World, Main!</string>
<string name="app_name">Color</string>
<drawable name="red">#ff0000</drawable>
<color name="gray">#999999</color>
<color name="blue">#0000ff</color>
<color name="background">#ffffff</color>
</resources></a>
上面定义了几个颜色值,下面是在布局文件中的调用,main.xml内容:
[java]
view plaincopy
<?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"
android:background="@color/background"
>
<TextView android:id="@+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textColor="@drawable/red"
/>
<TextView android:id="@+id/tv2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textColor="@color/gray"
/>
<TextView android:id="@+id/tv3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
在Java程序中使用:
[java]
view plaincopy
package com.pocketdigi.color;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
public class Main extends Activity {
/** Called when the activity is first created. */
TextView tv1,tv2,tv3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv1=(TextView)findViewById(R.id.tv1);
tv2=(TextView)findViewById(R.id.tv2);
tv3=(TextView)findViewById(R.id.tv3);
tv3.setTextColor(Color.BLUE);//直接使用android.graphics.Color的静态变量
tv2.setTextColor(this.getResources().getColor(R.color.blue));//使用预先设置的颜色值
}
}
下面内容看到有个哥们写的挺好的,就直接转载了(转自:http://blog.csdn.net/wangjia55/article/details/7815757)
android 各种控件颜色值的设置(使用Drawable,Color)
在Android中,如果需要改变控件默认的颜色,包括值的颜色,需要预先在strings.xml中设置,类似字符串,可以反复调用。Android中颜色可以使用drawable或是color来定义。本例中strings.xml内容:
view plaincopy
<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"?>
<resources>
<string name="hello">Hello World, Main!</string>
<string name="app_name">Color</string>
<drawable name="red">#ff0000</drawable>
<color name="gray">#999999</color>
<color name="blue">#0000ff</color>
<color name="background">#ffffff</color>
</resources></a>
上面定义了几个颜色值,下面是在布局文件中的调用,main.xml内容:
[java]
view plaincopy
<?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"
android:background="@color/background"
>
<TextView android:id="@+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textColor="@drawable/red"
/>
<TextView android:id="@+id/tv2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textColor="@color/gray"
/>
<TextView android:id="@+id/tv3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
在Java程序中使用:
[java]
view plaincopy
package com.pocketdigi.color;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
public class Main extends Activity {
/** Called when the activity is first created. */
TextView tv1,tv2,tv3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv1=(TextView)findViewById(R.id.tv1);
tv2=(TextView)findViewById(R.id.tv2);
tv3=(TextView)findViewById(R.id.tv3);
tv3.setTextColor(Color.BLUE);//直接使用android.graphics.Color的静态变量
tv2.setTextColor(this.getResources().getColor(R.color.blue));//使用预先设置的颜色值
}
}
相关文章推荐
- android 代码实现控件之间的间距
- 解決Linux下Android开发真机调试设备不被识别问题
- [Android]在代码里运行另一个程序的方法
- [软件咨询]WPS2012正式版已发布 金山Office移动版4.0发布
- Android笔记-Linux Kernel Ftrace (Function Trace)解析
- android USB如何修改VID具体实现
- Android Mouse实现过程详细笔记
- 深入Android Browser配置管理的详解
- Android Mms之:深入理解对话列表管理
- Android APP与媒体存储服务的交互
- android 多线程技术应用
- Android之采用execSQL与rawQuery方法完成数据的添删改查操作详解
- JQuery获取样式中的background-color颜色值的问题
- Android数据类型之间相互转换系统介绍
- 通过Android trace文件分析死锁ANR实例过程
- Android实现左右滑动效果的方法详解
- Android 各国语言缩写及简称详细介绍
- Android 自动判断是电话,网址,EMAIL方法之Linkify的使用
- android IPC之binder通信机制
- Android应用开发中模拟按下HOME键的效果(实现代码)