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

 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的形式调用


 


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值