我是新的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另一种方式...但如果你找不到任何,你可以使用这个:)