2011-12-12 122 views

回答

465

使用,..

Color.parseColor("#bdbdbd"); 

一样,

mTextView.setTextColor(Color.parseColor("#bdbdbd")); 
+4

为什么不'0xFFBDBDBD'? –

+3

Color.parseColor(“#FFBDBDBD”)也适用。 –

18
TextView tt; 
int color = Integer.parseInt("bdbdbd", 16)+0xFF000000; 
tt.setTextColor(color); 

tt.setBackgroundColor(Integer.parseInt("d4d446", 16)+0xFF000000); 

tt.setBackgroundColor(Color.parseColor("#d4d446")); 

看到:

Java/Android String to Color conversion

+0

@ ashish.n这样的变化应该作为一个新的答案发布,不管他是否想要回滚它,都会将它留给Marek。 – OGHaza

+1

@OGHaza感谢您的通知,我会允许编辑 –

32
yourTextView.setTextColor(color); 

或者,你的情况:yourTextView.setTextColor(0xffbdbdbd);

+0

0xff是什么意思?和最新的差异'setTextColor(0xffbdbdbd)'和'setTextColor(Color.parseColor(“#bdbdbd”))'? – mrid

+1

0xFFBDBDBD是整数的十六进制整数,其中不同部分表示alpha,红色,绿色和蓝色(0xAARRGGBB)的数量(0xff表示alpha完全是白色 - 即没有透明度)。 “#bdbdbd”是一个字符串,在传入函数'parseColor'时被解析为相同的整数。 – Jave

182

伟大的答案。添加one加载从Android资源XML中的颜色,但还是将其设置编程:

textView.setTextColor(getResources().getColor(R.color.some_color)); 

请注意,从API 23,getResources().getColor()已被弃用。改用:

textView.setTextColor(ContextCompat.getColor(context, R.color.some_color)); 

其中所需的颜色是XML定义为:

<resources> 
    <color name="some_color">#bdbdbd</color> 
</resources> 

更新:

此方法是在API级别23.的getColor弃用( int,Theme) 。

检查this

+0

谢谢,由于某种原因设置全色,比如'tv.setTextColor(color.holo_green_light)',我遇到了麻烦。文本显示不可见。因此,我将该颜色设置为XML中的资源,例如'@android:color/holo_green_light',并用它编程设置。来自API 23的 – RTF

+7

get.Color()已被弃用。 改为使用ContextCompat.getColor(context,R.color.color_name) – Rami

相关问题