用背景色在android中书写

用背景色在android中书写,android,textview,background-color,Android,Textview,Background Color,我是android开发的新手,我正在尝试用main.xml文件中设置的背景色编写一些东西/ 我的写作代码是 TextView textView = new TextView(this); textView.setText(s); setContentView(textView); 当我运行程序时,“setContentView”会覆盖背景色,是否有其他写入方式不会覆盖main.xml文件中写入的内容?您应该在xml文件中包含TextView,然后像这样引用TextView TextView t

我是android开发的新手,我正在尝试用main.xml文件中设置的背景色编写一些东西/ 我的写作代码是

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

当我运行程序时,“setContentView”会覆盖背景色,是否有其他写入方式不会覆盖main.xml文件中写入的内容?

您应该在xml文件中包含TextView,然后像这样引用TextView

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

必须有一种更简单的方法(可能有一个
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);
}
我确信还有另一种方法。。。但如果找不到,可以使用以下命令:)