【发布时间】:2021-05-01 23:36:29
【问题描述】:
我想以编程方式调整按钮数组中几个按钮的背景。在这样做时,我想从一个字符串和一个数字组装标识符。
对于strings.xml 中的文本资源,我已经能够在功能上将其组合在一起:
basicDEGR[g].setVoc(getResources().getIdentifier("vocGR" + (g +((category-1)*20)), "string", getPackageName()));
不幸的是,与 drawables 类似的方法不起作用:
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
Button button4 = (Button) findViewById(R.id.button4);
Button button5 = (Button) findViewById(R.id.button5);
Button button6 = (Button) findViewById(R.id.button6);
Button button7 = (Button) findViewById(R.id.button7);
Button button8 = (Button) findViewById(R.id.button8);
Button button9 = (Button) findViewById(R.id.button9);
Button button10 = (Button) findViewById(R.id.button10);
Button[] button = new Button[] {button1, button2, button3, button4, button5, button6, button7, button8, button9, button10};
for (int i = 1; i<11; i++)
{
button[i].setBackground(getResources().getIdentifier("gr" + i, "drawable", this.getPackageName()));
}
目前我遇到了这种错误:
error: incompatible types: int cannot be converted to Drawable
button[i].setBackground(getResources().getIdentifier("gr" + i, "drawable", this.getPackageName()));
^
什么是正确的方法?
【问题讨论】:
标签: java android android-layout button drawable