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

来源:互联网 发布:淘宝网退回旧版本 编辑:程序博客网 时间:2024/05/19 07:42
最近在做的项目包含了一个标题栏,可随着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>
例如,使用getBackground().setAlpha来改变ll_title_bar的透明度后,ll_content的透明度也会跟着被改变。


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

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

原文链接

0 0