国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android getBackground().setAlpha遇到問題解決辦法

2019-12-12 03:13:15
字體:
來源:轉載
供稿:網友

Android getBackground().setAlpha遇到問題解決辦法

前言:

使用getBackground().setAlpha,導致其他布局背景透明度都改變的問題

從晚上9點就開始琢磨,為什么我在一個地方設置了getBackground().setAlpha(0);在別的activity中有些控件也變成透明的了,讓我百思不得其解,哦,現在是晚上十一點四十五,問題終于解決(解決不了睡不著覺啊),覺得挺有意思的,分享一下,先舉個例子:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="match_parent"  android:layout_height="match_parent" >  <TextView    android:id="@+id/text1"    android:layout_width="match_parent"    android:layout_height="60dp"     android:background="@color/text_orange"    />  <TextView    android:id="@+id/text2"    android:layout_width="match_parent"    android:layout_height="60dp"    android:background="@color/text_orange"    /></LinearLayout>

兩個textview,background都指向相同的資源,那如果text1.getBackground().setAlpha(255)(不透明),那text2的背景是不是也跟著變成不透明的呢,答案是yes,那為什么呢:默認情況下,所有的從同一資源(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實例擁有同一個狀態,如果你在一個地方改變了狀態,其它的實例也會跟著改變。

OK。所以

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

問題解決了!

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 浦城县| 黄石市| 红原县| 佛冈县| 广东省| 平顺县| 巴南区| 大悟县| 永修县| 庆元县| 桂林市| 佛山市| 安平县| 马鞍山市| 霞浦县| 盖州市| 台中县| 阳江市| 互助| 南宁市| 惠水县| 马山县| 双鸭山市| 文水县| 西平县| 修文县| 洛南县| 星座| 曲阜市| 朝阳县| 秭归县| 五常市| 鸡泽县| 景宁| 美姑县| 宁化县| 朝阳市| 搜索| 景德镇市| 湘潭市| 东乡县|