我是新的android開發,我試着寫與背景顏色的東西,我在main.xml中文件中設置/ 我寫的代碼是這樣寫在android系統與背景顏色
TextView textView = new TextView(this);
textView.setText(s);
setContentView(textView);
當我運行程序時,「setContentView」會覆蓋背景顏色,還有另外一種寫法,不會重寫寫在main.xml文件中的內容嗎?
我是新的android開發,我試着寫與背景顏色的東西,我在main.xml中文件中設置/ 我寫的代碼是這樣寫在android系統與背景顏色
TextView textView = new TextView(this);
textView.setText(s);
setContentView(textView);
當我運行程序時,「setContentView」會覆蓋背景顏色,還有另外一種寫法,不會重寫寫在main.xml文件中的內容嗎?
你應該有TextView的在你的XML文件,然後引用您的TextView這樣
TextView textView = (TextView)findViewById(R.id.id_of_your_textview);
必須有一個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另一種方式...但如果你找不到任何,你可以使用這個:)