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

首頁 > 系統(tǒng) > Android > 正文

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

2019-10-23 18:34:55
字體:
供稿:網(wǎng)友

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

setButtonDrawable()的兼容問題

API16實現(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實現(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,允許使用者自行設置背景和按鈕的樣式,不過,有時我們僅希望簡單的設置一個有狀態(tài)的背景,并隱藏其默認樣式。可是,當我們調(diào)用setButtonDrawable(null)或setButtonDrawable(0)時,卻發(fā)現(xiàn)完全沒有效果。原來,CompoundButton的setButtonDrawable的代碼實現(xiàn)中屏蔽了null或resid為0的Drawable,迫使我們必須傳入有效的Drawable對象。

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

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

參考:

隱藏RadioButton, CheckBox圖片 setButtonDrawable:

RadioButton和CheckBox都是Android app中常用的Widget,它們派生于CompoundButton,允許使用者自行設置背景和按鈕的樣式,不過,有時我們僅希望簡單的設置一個有狀態(tài)的背景,并隱藏其默認樣式。可是,當我們調(diào)用setButtonDrawable(null)或setButtonDrawable(0)時,卻發(fā)現(xiàn)完全沒有效果。原來,CompoundButton的setButtonDrawable的代碼實現(xiàn)中屏蔽了null或resid為0的Drawable,迫使我們必須傳入有效的Drawable對象。

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

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

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

 

注:相關教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 勃利县| 安顺市| 杭州市| 禄劝| 德兴市| 青神县| 霍州市| 乐平市| 全椒县| 孟村| 城步| 西华县| 拉孜县| 钦州市| 休宁县| 和林格尔县| 田阳县| 西华县| 太康县| 义乌市| 西充县| 柯坪县| 丘北县| 福贡县| 卓资县| 耒阳市| 乾安县| 达日县| 吉安县| 岳阳市| 宝坻区| 银川市| 米泉市| 湖北省| 定安县| 兴隆县| 宝兴县| 东乡族自治县| 贡嘎县| 桃源县| 收藏|