setTextColor注意事项

我们直接对比下 setTextColor 和 setText 的源码:

//这里的color参数,是颜色值,而不是资源的ID

  public void setTextColor(int color) {

        mTextColor = ColorStateList.valueOf(color);

        updateTextColors();}

R.color.red通过eclipse生成的int值并不是真正的颜色值,必须通过mContext.getResources().getColorAndroid SDK 升級到 23 之後,ContextCompat.getColor(context, R.color.color_name)将真正的16进制颜色值#255#255#255#255值取得到才可以。

//这里的才是资源ID 

public final void setText(int resid) {

setText(getContext().getResources().getText(resid));}

我们只能这样:

Int color=context.getResources().getColor(R.Color.XXX); tv_text.setTextColor(color);

设置background(背景)的函数:

imageview.setBackgroundReource(int resid)  
imageview.setBackground(Drawable background)  
imageview.setBackgroundColor(int color)  


设置src(前景)的函数:

imageview.setImageDrawable(Drawable drawable);  
imageview.setImageBitmap(Bitmap bm);  
imageiew.setImageResource(int resId); 

Android代码中设置字体颜色的方法

TextView txt=(TextView) findViewById(R.id.text_name);
//第1种:
txt.setTextColor(android.graphics.Color.RED); // 系统自带的颜色类
//第2种:
txt.setTextColor(android.graphics.Color.parseColor("#87CEFA")); // 使用Color类转换
//第3种:
txt.setTextColor(0xffff00ff); // 0xffff00ff是int类型的数据,分组一下0x|ff|ff00ff,0x是代表颜色十六进制整数的标记,ff是表示透明度,ff00ff表示颜色。注意:这里ffff00ff必须是8位颜色格式,不接受ff00ff这种6位的颜色格式。
 
//第4种:
txt.setTextColor(this.getResources().getColor(R.color.red));
// 通过获得资源文件进行设置。根据不同的情况R.color.red也可以是R.string.red或者R.drawable.red,当然前提是需要在相应的配置文件里做相应的配置。如(xml 标签):
// <color name="red">#FF0000</color>
// <drawable name="red">#FF0000</drawable>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值