美文网首页
getBackground().setAlpha()导致其它标题

getBackground().setAlpha()导致其它标题

作者: 楷桐 | 来源:发表于2017-09-14 11:51 被阅读38次

最近在做的项目包含了一个标题栏,可随着ScrollView滑动而改变自身透明度。使用的正是getBackground().setAlpha来实现,在Android 5.0以下版本一直没问题,但在5.0以上系统时,就会导致其他共用一个资源的布局(例如:@color/white)透明度都跟对标题栏被改变了。

例如,使用getBackground().setAlpha来改变ll_title_bar的透明度后,ll_content的透明度也会跟着被改变。

在网上找了资料才知道,在布局中多个控件同时使用一个资源的时候,这些控件会共用一个状态,例如ColorState,如果你改变了一个控件的状态,其他的控件都会接收到相同的通知。这时我们可以使用mutate()方法使该控件状态不定,这样不定状态的控件就不会共享自己的状态了。

解决方法:

getBackground().setAlpha

改为

titleLayout.getBackground().mutate().setAlpha(255);

http://blog.csdn.net/myatlantis/article/details/49336587

如果以上方法不好使,那么就给这个标题栏单独设置一个背景(唯一的、不被共用的)。如下
    <RelativeLayout
        android:id="@+id/rl_toolbar"
        android:layout_width="match_parent"
        android:layout_height="54dp"
        android:background="@drawable/shape_toolbar_bg">

http://www.jianshu.com/p/65591a718cdc

相关文章

网友评论

      本文标题:getBackground().setAlpha()导致其它标题

      本文链接:https://www.haomeiwen.com/subject/icxdsxtx.html