我要建一个应用程序,有很多的屏幕。 大部分的屏幕有一个观点,在与背景颜色的顶端。
I'm building an app that has plenty of screens. most of the screens have a View at the top with a background color.
我经常用改变颜色view.setBackgroundColor(彩色)
I often change that color using view.setBackgroundColor(color)
下面来了奇怪的事情:有时会设置一个视图的颜色之后,说#f14fb7,在应用程序导航时,其他视图的背景被设置为颜色没有我想他们。 它有时甚至会发生意见我没有设定一个id,所以也没有办法setBackgroundColor拜访了这些看法。
Here comes the weird thing: sometimes after setting the color of one view, say to #f14fb7 , when navigating in the app, other views' backgrounds are set to that color without me wanting them to. It sometimes even happens to views I had not set an id for, so there is no way setBackgroundColor was called on those views.
这种情况很少见,而不是一致的任何流量我试过了。
This happens rarely and is not consistant to any flow I tried.
当我杀了应用程序,并重新启动它,一切正常,因为它应该。
When I kill the app and restart it, everything works as it should.
我唯一的猜测是内存泄漏的一些国王,但我希望有一个更简单的解释。
My only guess is some king of memory leak, but I hope there is a simpler explanation.
谁能想到某种原因,要做到这一点?
Can anyone think of some reason for this to happen?
*它发生在我的Galaxy S3。
*It happens on my Galaxy S3.
推荐答案
无C吧$ C $是不容易的......但我猜你是重复使用相同的 ColorDrawable
在多个视图,如果你看看 View.setBackgroundColor()
源$ C $ C:
Without the code it's not easy... but I guess you are reusing the same ColorDrawable
on multiple views and if you take a look at View.setBackgroundColor()
source code :
public void setBackgroundColor(int color) {
if (mBGDrawable instanceof ColorDrawable) {
((ColorDrawable) mBGDrawable).setColor(color);
} else {
setBackgroundDrawable(new ColorDrawable(color));
}
}
您可以看到,它改变了 ColorDrawable
的颜色,不要每次都创建一个新的。我是pretty的肯定,这就是为什么你有这样奇怪的行为。
You can see that it change the color of the ColorDrawable
and don't create a new one each time. I'm pretty sure this is why you have this strange behavior.
修改
当您设置XML中的初始背景颜色与安卓背景
你这样做(据android DOC ):
When you set the initial background color in xml with android:background
you are doing this (according android doc):
将背景设为一个给定的资源。资源应该是指一个可绘制对象
Set the background to a given resource. The resource should refer to a Drawable object
据我的理解,将在充气过程中设置的字段 View.mBGDrawable
。我建议你使用 View.setBackgoundDrawable(新ColorDrawable(the_color_int_ code)))
而不是 setBackgroung(the_color_int_ code)
。它应该解决您的问题。
According my understanding it will set the field View.mBGDrawable
during the inflate. I suggest you to use View.setBackgoundDrawable(new ColorDrawable(the_color_int_code)))
instead of setBackgroung(the_color_int_code)
. It should solve your issue.
相关推荐
最新文章