我想制作一个textView
,它在数字为正数时变绿色,在数字为负数时变红,在数字为0时不可见。
我已将updateView.setDrawable(@Drawable/add_background)
更改为updateView.setDrawable(R.drawable.add_background)
,现在收到错误'setBackground(android.graphics.drawable.Drawable)' in 'android.view.View' cannot be applied to '(int)'
代码如下:
Integer team1UpdateAmount = 0;
TextView updateView = findViewById(R.id.team1UpdateView);
if (team1UpdateAmount == 0) {updateView.setVisibility(View.INVISIBLE); return;}
updateView.setVisibility(View.VISIBLE);
if (team1UpdateAmount > 0) {
updateView.setText("+" + team1UpdateAmount);
updateView.setBackground(R.drawable.add_background);
}
if (team1UpdateAmount < 0) {
updateView.setText("" + team1UpdateAmount);
updateView.setBackground(R.drawable.sub_background);
}
2条答案
按热度按时间nhaq1z211#
看看这条线
这就是在代码中引用资源的方式,
@
引用用于XMLdgsult0t2#
在实验中,我找到了答案。
我将
getResources().getDrawable(R.drawable.add_background)
设置为名为addBackground
的变量(与sub相同),并在updateView.setBackground(addBackground)
中引用该变量代码现在看起来像这样: