用android编写背景颜色

时间:2011-10-22 17:43:41

标签: android textview background-color

我是Android开发的新手,我正在尝试使用我在main.xml文件中设置的背景颜色来编写/ 我的写作代码就是这个

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

当我运行程序时,“setContentView”会覆盖背景颜色,还有另一种编写方式,它不会写入main.xml文件中的内容吗?

2 个答案:

答案 0 :(得分:1)

您应该在XML文件中使用TextView,然后像这样引用TextView

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

答案 1 :(得分:1)

必须有一种更简单的方法(也许是你可以改变的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);
}

肯定还有另一种方式......但是如果你找不到任何东西,你可以使用它:)