getBackground().setAlpha所导致问题
来源:互联网 发布:端口被系统占用 编辑:程序博客网 时间:2024/06/04 18:35
在项目中遇到一个问题,用framelayout装载三个Fragment,每个fragment都有一个titleBar,每个titlebar的背景颜色一样,我想在第一个fragemnt中设置titlebar的透明度为50,于是我是这样设置的:
mTitlebar.getBackground.setAlpha(50);
那么问题来了,运行项目,发现每个三个titleBar的背景都改变了。
于是乎,运用强大的百度一下,终于解决了这个问题,看来看源码很重要啊。。。。
解决办法:
mTitlebar.getBackground().mutate().setAlpha(50);
那为什么呢:默认情况下,所有的从同一资源(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实例拥有同一个状态,如果你在一个地方改变了状态,其它的实例也会跟着改变。
0 0
- getBackground().setAlpha所导致问题
- getBackground().setAlpha所导致问题
- getBackground().setAlpha所导致问题
- getBackground().setAlpha所导致问题
- getBackground().setAlpha 奇葩问题
- 使用getBackground().setAlpha,导致其他布局背景透明度都改变的问题
- 使用getBackground().setAlpha,导致其他布局背景透明度都改变的问题
- 使用getBackground().setAlpha,导致其他布局背景透明度都改变的问题
- 使用getBackground().setAlpha,导致其他布局背景透明度都改变的问题
- getBackground().setAlpha导致其他布局背景透明度都改变的问题
- 使用getBackground().setAlpha,导致其他布局背景透明度都改变的问题
- getBackground().setAlpha导致所有资源都有变化
- getBackground().setAlpha和setAlpha有什么区别
- android 控件 getBackground().setAlpha设置透明值,影响其他页面样式问题
- view.setAlpha()和 view.getBackground().setAlpha()的区别
- getBackground().setAlpha(ScrollY)遇到的奇怪问题解决方案
- 慎用alpha值——setAlpha与getBackground.setAlpha的差别
- getBackground().setAlpha(0)设置透明度连下层控件背景色都被改变
- java获取当前日期是星期几
- Mainfest.xml文件的初步解析
- 今年暑假不ac
- uva10816 Travel in Desert(MST + 最短路)
- WordPress密码哈希破解
- getBackground().setAlpha所导致问题
- ArrayList源码分析
- hdu 3746 Cyclic Nacklace(kmp)
- Spring MVC静态资源处理<mvc:resources><mvc:default-servlet-handler>
- shell(二)Bash常用快捷键
- 结构体的学习
- 【BZOJ1068】[SCOI2007]压缩【区间DP】
- Activity中获取view的高度和宽度为0的原因以及解决方案
- linux服务器部署lamp