1、问题:
在Android 5.0以下版本一直没问题,但在5.0以上系统时,就会导致其他共用一个资源的布局(例如:@color/white)透明度都跟对标题栏被改变了。
如:mLayout.getBackground().setAlpha(0),那么所有的背景就为透明了
2、产生的原因
默认情况下,所有的从同一资源(R.drawable.***等等)加载的实例都共享一个共用的状态,如果你更改一个实例的状态,其余的实例都会接收到相同的通知。
**
* Make this drawable mutable. This operation cannot be reversed. A mutable
* drawable is guaranteed to not share its state with any other drawable.
* This is especially useful when you need to modify properties of drawables
* loaded from resources. By default, all drawables instances loaded from
* the same resource share a common state; if you modify the state of one
* instance, all the other instances will receive the same modification.
*
* Calling this method on a mutable Drawable will have no effect.
*
* @return This drawable.
* @see ConstantState
* @see #getConstantState()
*/
public Drawable mutate() {
return this;
}
翻译一下注释吧:让这个drawable可变,这个操作是不可逆的。一个可变Drawable可以保证不与其它的Drawable分享一个状态。当你需要修改资源中的Drawable的属性时这个方法是非常有用的,因为默认情况下加载相同资源的所有Drawable实例拥有同一个状态,如果你在一个地方改变了状态,其它的实例也会跟着改变。
3.解决办法:
我们可以使用mutate()方法使该控件状态不定,这样不定状态的控件就不会共享自己的状态了。
如:mLayout.getBackground().mutate().setAlpha(0);