Android getBackground().setAlpha遇到問(wèn)題解決辦法
Android getBackground().setAlpha遇到問(wèn)題解決辦法
前言:
使用getBackground().setAlpha,導(dǎo)致其他布局背景透明度都改變的問(wèn)題
從晚上9點(diǎn)就開(kāi)始琢磨,為什么我在一個(gè)地方設(shè)置了getBackground().setAlpha(0);在別的activity中有些控件也變成透明的了,讓我百思不得其解,哦,現(xiàn)在是晚上十一點(diǎn)四十五,問(wèn)題終于解決(解決不了睡不著覺(jué)?。?,覺(jué)得挺有意思的,分享一下,先舉個(gè)例子:
<?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>
兩個(gè)textview,background都指向相同的資源,那如果text1.getBackground().setAlpha(255)(不透明),那text2的背景是不是也跟著變成不透明的呢,答案是yes,那為什么呢:默認(rèn)情況下,所有的從同一資源(R.drawable.***等等)加載的實(shí)例都共享一個(gè)共用的狀態(tài),如果你更改一個(gè)實(shí)例的狀態(tài),其余的實(shí)例都會(huì)接收到相同的通知。
那怎么解決這種情況呢,看看這個(gè)方法:
/**
* 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;
}
翻譯一下注釋吧:讓這個(gè)drawable可變,這個(gè)操作是不可逆的。一個(gè)可變Drawable可以保證不與其它的Drawable分享一個(gè)狀態(tài)。當(dāng)你需要修改資源中的Drawable的屬性時(shí)這個(gè)方法是非常有用的,因?yàn)槟J(rèn)情況下加載相同資源的所有Drawable實(shí)例擁有同一個(gè)狀態(tài),如果你在一個(gè)地方改變了狀態(tài),其它的實(shí)例也會(huì)跟著改變。
OK。所以
text1.getBackground().mutate().setAlpha(255);
問(wèn)題解決了!
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
android獲取屏幕高度和寬度的實(shí)現(xiàn)方法
這篇文章主要介紹了android獲取屏幕高度和寬度的實(shí)現(xiàn)方法,較為詳細(xì)的分析了Android獲取屏幕高度和寬度的原理與實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01
簡(jiǎn)單實(shí)現(xiàn)Android彈出菜單效果
這篇文章主要為大家詳細(xì)介紹了簡(jiǎn)單實(shí)現(xiàn)Android彈出菜單效果的相關(guān)代碼,感興趣的小伙伴們可以參考一下2016-06-06
利用Jetpack Compose實(shí)現(xiàn)經(jīng)典俄羅斯方塊游戲
你的童年是否有俄羅斯方塊呢,本文就來(lái)介紹如何通過(guò)Jetpack Compose實(shí)現(xiàn)一個(gè)俄羅斯方塊!感興趣的小伙伴快跟隨小編一起動(dòng)手嘗試一下吧2022-05-05
Android 加載大圖及多圖避免程序出現(xiàn)OOM(OutOfMemory)異常
這篇文章主要介紹了Android 加載大圖及多圖避免程序出現(xiàn)OOM(OutOfMemory)異常的相關(guān)資料,需要的朋友可以參考下2017-03-03
android調(diào)用國(guó)家氣象局天氣預(yù)報(bào)接口json數(shù)據(jù)格式解釋
平時(shí)我們?cè)陂_(kāi)發(fā)的過(guò)程中有時(shí)會(huì)要用到天氣預(yù)報(bào)的信息,國(guó)家氣象局為我們提供了天氣預(yù)報(bào)的接口,只需要我們?nèi)ソ馕鼍托辛?。很方便很好?/div> 2013-11-11
android實(shí)現(xiàn)簡(jiǎn)單的畫(huà)畫(huà)板實(shí)例代碼
畫(huà)畫(huà)板實(shí)現(xiàn)起來(lái)其實(shí)很簡(jiǎn)單,我們只需要利用android給我們提供的Canvas類來(lái)操作就可以實(shí)現(xiàn)簡(jiǎn)單的畫(huà)畫(huà)功能2014-01-01
Android開(kāi)發(fā)中TextView文本過(guò)長(zhǎng)滾動(dòng)顯示實(shí)現(xiàn)方法分析
這篇文章主要介紹了Android開(kāi)發(fā)中TextView文本過(guò)長(zhǎng)滾動(dòng)顯示實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android項(xiàng)目開(kāi)發(fā)中TextView顯示超長(zhǎng)文本的具體操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-02-02最新評(píng)論

