2011-02-02 50 views
1

我以編程方式將TextView添加到線性佈局。我可以看到背景圖片,但由於某種原因,文本從不顯示。有任何想法嗎?代碼如下:在TextView中添加以編程方式顯示沒有文字

TextView pointsTV = new TextView(getApplicationContext()); 
pointsTV.setText("Test Should Show"); 
pointsTV.setGravity(Gravity.CENTER); 
pointsTV.setTextColor(android.R.color.white); 
pointsTV.setPadding(0, 20, 0, 20); 
pointsTV.setBackgroundResource(R.drawable.gamesummary_bottom); 
pointsTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
ll.addView(pointsTV); 

回答

0

您正在將顏色設置爲數字值,導致顏色消失。使用

pointsTV.setBackgroundResource(android.R.color.white); 

爲了使用setTextColor(),你必須給它一個statelist或十六進制,而不是顏色值。 The salient bit of the developer guide is here.

+1

就是這樣! pointsTV.setTextColor(ColorStateList.valueOf(Color.WHITE));謝謝! – Graeme 2011-02-02 22:10:07