我正在尝试恢复 View 的背景颜色。
我有几个可选择的 View 。当用户单击其中一个 View 时,将执行以下代码并且 View 变为黄色:
View newSelection, previousSelection;
...
if(previousSelection != null) {
previousSelection.setBackgroundColor(Color.BLACK); // problem here
}
newSelection.setBackgroundColor(Color.YELLOW);
但是,我想重新设置之前选择的 View 的颜色。但是,我不知道它是哪种颜色(我在上面的代码中将它设置为 Color.BLACK)。我无法在 View 类中找到 getBackgroundColor 或类似方法。如果我有它,我可以保存以前的颜色,并在选择新 View 时将其放回原处。
最佳答案
使用 View.getBackground(),它返回 View 的当前 'Drawable' 背景,然后可以在 View 中使用。setBackgroundDrawable()
View theView;
Drawable originalBackground;
...
originalBackground = theView.getBackground();
theView.setBackgroundColor(Color.YELLOW);
...
theView.setBackgroundDrawable(originalBackground);
关于android - 重置 View 的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3671357/