4、android xml中drawableTop(drawableBoottom、drawableLeft、drawableRight)在java代碼中的動態配置

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   使用   sp   java   on   


 

做安卓開發的朋友都知道,我們在xml中可以通過這樣來對button設定其上部或者(下、左、右)的圖片資源:

 

那麼如果需要動態配置圖片呢?我們不得不使用java代碼來進行操作:

Drawable drawable = null;drawable=splash.contextTools.getResources().getDrawable(R.drawable.pepper);//得到drawable對象drawable.setBounds(0, 0, 24,75);//設定drawable對象的寬度和高度                              new_button.setCompoundDrawables(null, null, drawable, null);//將drawable對象放在new_button的右側(這裡有個注意點,如果使用setCompundDrawables方法,一定要首先通過setBounds來配置一片資源)new_button.setCompoundDrawablePadding(35); //設定drawable與new_button的間距

 

通過以上代碼,就基本滿足我們的使用需求了。

有些同學估計會問,如果我要保證圖片原始大小怎麼辦呢?

setCompoundDrawablesWithIntrinsicBounds(null,null,drawable,null);//這個方法可以使用圖片固有的寬度和高度

ok,下次再分享其他的

4、android xml中drawableTop(drawableBoottom、drawableLeft、drawableRight)在java代碼中的動態配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.