您的位置:首页 > 移动开发 > Android开发

android学习之三:如何使用自定义颜色

2011-10-22 21:03 561 查看
 1.color.xml里面定义颜色

<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="darkgray">#808080FF</drawable>
<drawable name="white">#FFFFFFFF</drawable>
<drawable name="bule">#0000FF</drawable>
</resources>

 

2.在main.xml里面使用,通过工程运行直接显示

 

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="all"
android:text="百度:http://www.baidu.com"
android:layout_x="10px"
android:layout_y="100px"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="百度:http://www.baidu.com"
android:textColor="@drawable/darkgray"
android:background="@drawable/white"
android:layout_x="10px"
android:layout_y="200px"
/>
</AbsoluteLayout>


3.如何通过程序时间来改变字体的颜色

 

Resources resource=this.getBaseContext().getResources();//this.getResources();
Drawable drawable=resource.getDrawable(R.drawable.white);

TextView tw=(TextView) this.findViewById(R.id.text);
tw.setBackgroundDrawable(drawable);
/* tw.setBackgroundColor(Color.WHITE);
tw.setBackgroundResource(R.drawable.white);*/


 

总结:1.xml里面是配置自定义颜色的格式

<resources>

  <drawable name="颜色名">#颜色值</drawable>

</resources>

2.在xml里面引用

<TextView 

     ....................

    android:textColor="@drawable/darkgray"

    android:background="@drawable/white"

    .....................

    />

在根据字符串的引用:

android:tex="@string/字符串名" 

xml定义

strings.xml

<resources>

    <string name="字符串名称">字符串</string>

</resources>

可以发现,这些资源的定义格式和引用的区别,只有xml文件中元素不同而已,

在用的时候不需要去调用文件名,因为这些元素直接R文件中以ID的形式出现了

3.在程序中引用

Drawable drawable=resource.getDrawable(R.drawable.white);

 tw.setBackgroundDrawable(drawable);

4.这里关于设置TextView的背景颜色的三种方法

  void setBackgroundColor(int color);

       void setBackgroundDrawable(Drawable d);

       void setBackgroundResource(int resid);

------------------------------------------------

 tw.setBackgroundDrawable(drawable);//这里是引用自定义的资源文件

      tw.setBackgroundColor(Color.WHITE);//这里则直接用Color类里面的值

      tw.setBackgroundResource(R.drawable.white);//这里则以ID的形式调用

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐