您的位置:首页 > 其它

使用getBackground().setAlpha,导致其他布局背景透明度都改变的问题

2017-12-20 09:46 267 查看
最近在做的项目包含了一个标题栏,可随着ScrollView滑动而改变自身透明度。使用的正是getBackground().setAlpha来实现,在Android 5.0以下版本一直没问题,但在5.0以上系统时,就会导致其他共用一个资源的布局(例如:@color/white)透明度都跟对标题栏被改变了。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
android:id="@+id/ll_title_bar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/white"
android:orientation="vertical" />

<LinearLayout
android:id="@+id/ll_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical" />

</LinearLayout>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

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

在网上找了资料才知道,在布局中多个控件同时使用一个资源的时候,这些控件会共用一个状态,例如ColorState,如果你改变了一个控件的状态,其他的控件都会接收到相同的通知。这时我们可以使用mutate()方法使该控件状态不定,这样不定状态的控件就不会共享自己的状态了。
titleLayout.getBackground().mutate().setAlpha(255);
原文地址:http://blog.csdn.net/myatlantis/article/details/49336587
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐