android - 重置 View 的背景颜色

标签 android view background-color

我正在尝试恢复 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/

相关文章:

android - 将相机移动到 map View 上的某个位置

ios - View 层次结构不正确 : Presenting view controllers on detached view controllers is discouraged

html - alpha是如何在CSS中添加的?

css - 背景颜色未填满整个 li

css - UL 和 float DIV : list text wraps but background color doesn't

java - gradle 在 Android Studio 中一直失败

android - 无按钮爆破相机

android - 将 DrawerLayout 与 fragment 一起使用时 ListView 重影

javascript - 在 React Native 中渲染另一个 View

java - Android View.GONE 选项和整体应用程序速度