使用getBackground().setAlpha,导致其他布局背景透明度都改变的问题
2017-12-20 09:46
267 查看
最近在做的项目包含了一个标题栏,可随着ScrollView滑动而改变自身透明度。使用的正是getBackground().setAlpha来实现,在Android 5.0以下版本一直没问题,但在5.0以上系统时,就会导致其他共用一个资源的布局(例如:@color/white)透明度都跟对标题栏被改变了。
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()方法使该控件状态不定,这样不定状态的控件就不会共享自己的状态了。
<?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
相关文章推荐
- 使用getBackground().setAlpha,导致其他布局背景透明度都改变的问题
- 使用getBackground().setAlpha,导致其他布局背景透明度都改变的问题
- 使用getBackground().setAlpha,导致其他布局背景透明度都改变的问题
- 使用getBackground().setAlpha,导致其他布局背景透明度都改变的问题
- getBackground().setAlpha导致其他布局背景透明度都改变的问题
- 解决使用seletor点击改变背景时事件被上层布局获取的问题
- 部分机型带透明度背景使用popupwindow 的时候,出现闪屏问题(转自简书)
- Android之使用参数改变ProgressDialog的位置、大小、背景透明度、屏幕透明度
- getBackground().setAlpha(0)设置透明度连下层控件背景色都被改变
- 解决系统改变字体大小的时候导致的界面布局混乱的问题
- Android解决系统改变字体大小的时候导致的界面布局混乱的问题
- Android在监听器中使用代码改变其他控件背景或颜色
- 使用Runtime解决 cell 点击时子视图改变背景颜色的问题
- 使用listview,item的按钮点击改变按钮背景,点击其他条目按钮还原
- Android之使用参数改变ProgressDialog的位置、大小、背景透明度、屏幕透明度
- 改变navigationBar的背景导致其他控件被覆盖
- android ActivityGroup横竖屏使用不同布局导致的问题
- Android之使用参数改变ProgressDialog的位置、大小、背景透明度、屏幕透明度
- xml布局文件里直接使用背景图导致爆内存的情况处理
- eclipse cdt 使用其他主题导致 elif 宏 代码库 看不清问题