Android setButtonDrawable()的兼容問(wèn)題解決辦法
Android setButtonDrawable()的兼容問(wèn)題解決辦法
setButtonDrawable()的兼容問(wèn)題
API16實(shí)現(xiàn)
/**
* Set the background to a given Drawable, identified by its resource id.
*
* @param resid the resource id of the drawable to use as the background
*/
public void setButtonDrawable(int resid) {
if (resid != 0 && resid == mButtonResource) {
return;
}
mButtonResource = resid;
Drawable d = null;
if (mButtonResource != 0) {
d = getResources().getDrawable(mButtonResource);
}
setButtonDrawable(d);
}
/**
* Set the background to a given Drawable
*
* @param d The Drawable to use as the background
*/
public void setButtonDrawable(Drawable d) {
if (d != null) {
if (mButtonDrawable != null) {
mButtonDrawable.setCallback(null);
unscheduleDrawable(mButtonDrawable);
}
d.setCallback(this);
d.setState(getDrawableState());
d.setVisible(getVisibility() == VISIBLE, false);
mButtonDrawable = d;
mButtonDrawable.setState(null);
setMinHeight(mButtonDrawable.getIntrinsicHeight());
}
refreshDrawableState();
}
API23實(shí)現(xiàn)
/**
* Sets a drawable as the compound button image given its resource
* identifier.
*
* @param resId the resource identifier of the drawable
* @attr ref android.R.styleable#CompoundButton_button
*/
public void setButtonDrawable(@DrawableRes int resId) {
final Drawable d;
if (resId != 0) {
d = getContext().getDrawable(resId);
} else {
d = null;
}
setButtonDrawable(d);
}
/**
* Sets a drawable as the compound button image.
*
* @param drawable the drawable to set
* @attr ref android.R.styleable#CompoundButton_button
*/
@Nullable
public void setButtonDrawable(@Nullable Drawable drawable) {
if (mButtonDrawable != drawable) {
if (mButtonDrawable != null) {
mButtonDrawable.setCallback(null);
unscheduleDrawable(mButtonDrawable);
}
mButtonDrawable = drawable;
if (drawable != null) {
drawable.setCallback(this);
drawable.setLayoutDirection(getLayoutDirection());
if (drawable.isStateful()) {
drawable.setState(getDrawableState());
}
drawable.setVisible(getVisibility() == VISIBLE, false);
setMinHeight(drawable.getIntrinsicHeight());
applyButtonTint();
}
}
}
結(jié)論
RadioButton和CheckBox都是Android app中常用的Widget,它們派生于CompoundButton,允許使用者自行設(shè)置背景和按鈕的樣式,不過(guò),有時(shí)我們僅希望簡(jiǎn)單的設(shè)置一個(gè)有狀態(tài)的背景,并隱藏其默認(rèn)樣式??墒?,當(dāng)我們調(diào)用setButtonDrawable(null)或setButtonDrawable(0)時(shí),卻發(fā)現(xiàn)完全沒(méi)有效果。原來(lái),CompoundButton的setButtonDrawable的代碼實(shí)現(xiàn)中屏蔽了null或resid為0的Drawable,迫使我們必須傳入有效的Drawable對(duì)象。
這時(shí)候,透明顏色就可以派上用場(chǎng)了:
button.setButtonDrawable(new ColorDrawable(Color.TRANSPARENT));
參考:
隱藏RadioButton, CheckBox圖片 setButtonDrawable:
RadioButton和CheckBox都是Android app中常用的Widget,它們派生于CompoundButton,允許使用者自行設(shè)置背景和按鈕的樣式,不過(guò),有時(shí)我們僅希望簡(jiǎn)單的設(shè)置一個(gè)有狀態(tài)的背景,并隱藏其默認(rèn)樣式??墒?,當(dāng)我們調(diào)用setButtonDrawable(null)或setButtonDrawable(0)時(shí),卻發(fā)現(xiàn)完全沒(méi)有效果。原來(lái),CompoundButton的setButtonDrawable的代碼實(shí)現(xiàn)中屏蔽了null或resid為0的Drawable,迫使我們必須傳入有效的Drawable對(duì)象。
這時(shí)候,透明顏色就可以派上用場(chǎng)了:
button.setButtonDrawable(new ColorDrawable(Color.TRANSPARENT));
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Android實(shí)現(xiàn)輸入法彈出時(shí)把布局頂上去和登錄按鈕頂上去的解決方法
- android虛擬鍵盤(pán)彈出遮擋登陸按鈕問(wèn)題的解決方法
- Android防止按鈕過(guò)快點(diǎn)擊造成多次事件的解決方法
- Android Button 自帶陰影效果另一種解決辦法
- Android中EditText+Button組合導(dǎo)致輸入板無(wú)法收起的原因分析及解決辦法
- 實(shí)例詳解Android解決按鈕重復(fù)點(diǎn)擊問(wèn)題
- Android開(kāi)發(fā)軟鍵盤(pán)遮擋登陸按鈕的完美解決方案
- Android優(yōu)雅的方式解決軟鍵盤(pán)遮擋按鈕問(wèn)題
- Android編程出現(xiàn)Button點(diǎn)擊事件無(wú)效的解決方法示例
相關(guān)文章
Android 點(diǎn)擊ImageButton時(shí)有“按下”的效果的實(shí)現(xiàn)
這篇文章主要介紹了 Android 點(diǎn)擊ImageButton時(shí)有“按下”的效果的實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-03-03
Android?Flutter控件封裝之視頻進(jìn)度條的實(shí)現(xiàn)
這篇文章主要來(lái)和大家分享一個(gè)很簡(jiǎn)單的控制器封裝案例,包含了基本的播放暫停,全屏和退出全屏,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2023-06-06
Android Studio使用教程(二):基本設(shè)置與運(yùn)行
這篇文章主要介紹了Android Studio使用教程(二):基本設(shè)置與運(yùn)行,本文講解了項(xiàng)目結(jié)構(gòu)、偏好設(shè)置、常用功能介紹、創(chuàng)建模擬器等內(nèi)容,需要的朋友可以參考下2015-05-05
Flutter多項(xiàng)選擇彈窗實(shí)現(xiàn)詳解
這篇文章介紹了Flutter多項(xiàng)選擇彈窗實(shí)現(xiàn)詳解,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧<BR>2021-11-11
android開(kāi)發(fā)教程之使用listview顯示qq聯(lián)系人列表
這篇文章主要介紹了android使用listview顯示qq聯(lián)系人列表的示例,需要的朋友可以參考下2014-02-02
flutter Container容器實(shí)現(xiàn)圓角邊框
這篇文章主要為大家詳細(xì)介紹了flutter Container容器實(shí)現(xiàn)圓角邊框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
Android 內(nèi)存溢出和內(nèi)存泄漏的問(wèn)題
這篇文章主要介紹了Android 內(nèi)存溢出和內(nèi)存泄漏的問(wèn)題的相關(guān)資料,需要的朋友可以參考下2017-03-03
Android開(kāi)發(fā)中使用Volley庫(kù)發(fā)送HTTP請(qǐng)求的實(shí)例教程
這篇文章主要介紹了Android開(kāi)發(fā)中使用Volley庫(kù)發(fā)送HTTP請(qǐng)求的實(shí)例教程,包括創(chuàng)建Volley單例的基本知識(shí)與取消Request請(qǐng)求的技巧等,需要的朋友可以參考下2016-05-05

