Android setButtonDrawable()的相容問題解決辦法

NO IMAGE

Android  setButtonDrawable()的相容問題解決辦法

setButtonDrawable()的相容問題

API16實現


/**
* 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實現


/**
* 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();
}
}
}

結論

RadioButton和CheckBox都是Android app中常用的Widget,它們派生於CompoundButton,允許使用者自行設定背景和按鈕的樣式,不過,有時我們僅希望簡單的設定一個有狀態的背景,並隱藏其預設樣式。可是,當我們呼叫setButtonDrawable(null)或setButtonDrawable(0)時,卻發現完全沒有效果。原來,CompoundButton的setButtonDrawable的程式碼實現中遮蔽了null或resid為0的Drawable,迫使我們必須傳入有效的Drawable物件。

這時候,透明顏色就可以派上用場了:

button.setButtonDrawable(new ColorDrawable(Color.TRANSPARENT));

參考:

隱藏RadioButton, CheckBox圖片 setButtonDrawable:

RadioButton和CheckBox都是Android app中常用的Widget,它們派生於CompoundButton,允許使用者自行設定背景和按鈕的樣式,不過,有時我們僅希望簡單的設定一個有狀態的背景,並隱藏其預設樣式。可是,當我們呼叫setButtonDrawable(null)或setButtonDrawable(0)時,卻發現完全沒有效果。原來,CompoundButton的setButtonDrawable的程式碼實現中遮蔽了null或resid為0的Drawable,迫使我們必須傳入有效的Drawable物件。

這時候,透明顏色就可以派上用場了:


button.setButtonDrawable(new ColorDrawable(Color.TRANSPARENT)); 

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

您可能感興趣的文章:

Android實現輸入法彈出時把佈局頂上去和登入按鈕頂上去的解決方法android虛擬鍵盤彈出遮擋登陸按鈕問題的解決方法Android防止按鈕過快點選造成多次事件的解決方法Android Button 自帶陰影效果另一種解決辦法Android中EditText Button組合導致輸入板無法收起的原因分析及解決辦法例項詳解Android解決按鈕重複點選問題Android開發軟鍵盤遮擋登陸按鈕的完美解決方案Android優雅的方式解決軟鍵盤遮擋按鈕問題Android程式設計出現Button點選事件無效的解決方法示例