如何解決在Android中使用setButtonDrawable()方法出現(xiàn)的兼容問題

這篇文章給大家介紹如何解決在Android中使用setButtonDrawable()方法出現(xiàn)的兼容問題,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

目前創(chuàng)新互聯(lián)已為上千的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機(jī)、網(wǎng)站托管、服務(wù)器租用、企業(yè)網(wǎng)站設(shè)計(jì)、仁和網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

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

setButtonDrawable()的兼容問題

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

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

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

參考:

隱藏RadioButton, CheckBox圖片 setButtonDrawable:

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

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

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

關(guān)于如何解決在Android中使用setButtonDrawable()方法出現(xiàn)的兼容問題就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

分享標(biāo)題:如何解決在Android中使用setButtonDrawable()方法出現(xiàn)的兼容問題
地址分享:http://www.muchs.cn/article30/gjjoso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、網(wǎng)站設(shè)計(jì)響應(yīng)式網(wǎng)站云服務(wù)器、域名注冊微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名