2011-10-22 46 views
1

我是新的android開發,我試着寫與背景顏色的東西,我在main.xml中文件中設置/ 我寫的代碼是這樣寫在android系統與背景顏色

TextView textView = new TextView(this); 
textView.setText(s); 
setContentView(textView); 

當我運行程序時,「setContentView」會覆蓋背景顏色,還有另外一種寫法,不會重寫寫在main.xml文件中的內容嗎?

回答

1

你應該有TextView的在你的XML文件,然後引用您的TextView這樣

TextView textView = (TextView)findViewById(R.id.id_of_your_textview); 
1

必須有一個simplier方式(或許那裏有說明你可以改變的LinearLayout的android:background屬性)

但我現在能想到的唯一的事情是,你的情況下,子類的TextView和實施onDraw這樣:

public void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    canvas.clipRect(new Rect(0, 0, textViewWidth, textViewHeight), Region.Op.REPLACE); 
    canvas.drawColor(yourBgColor); 
} 

我是當然 theres另一種方式...但如果你找不到任何,你可以使用這個:)