TextView setBackground()不起作用

时间:2012-10-30 18:06:50

标签: android textview

我是Android新手,我遇到以下问题:

我想将TextView的背景设置为#333。我用过:

    TextView title = new TextView(this);  
    title.setText(currentContinent); 
    title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
    title.setBackgroundColor(0x333333);
    title.setGravity(Gravity.CENTER);

尽管Eclipse没有标记错误,但TextView仍然具有透明背景。我做错了什么?

4 个答案:

答案 0 :(得分:3)

我有一个类似的问题,我在创建数字颜色而不考虑前导alpha通道。即。 mytext.setTextColor(0xFF0000)(认为这会是红色的)。虽然这是一种红色,但它也是100%透明的,因为它= 0x00FF0000;正确的100%不透明值为0xFFFF0000mytext.setTextcolor(0xFFFF0000).

在您的情况下,请:mytext.setTextcolor(0x33333333)

答案 1 :(得分:1)

尝试这样的事情:

title.setBackgroundColor(Color.rgb(51, 51, 51));

以十进制形式提供rgb组件值。

答案 2 :(得分:0)

TextView title = new TextView(this);  
title.setText(currentContinent); 
title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
title.setBackgroundColor(0x33333333);
title.setGravity(Gravity.CENTER);

试试这个..,。

答案 3 :(得分:0)

您可以使用以下颜色设置任何颜色:

txt.setBackgroundColor(Color.parseColor("#BCDABA")); // set any custom color as  
//background    color 

txt.setBackgroundColor(Color.GREEN); // set default GREEN color as background color
相关问题