Android在代碼中設置控件的drawableLeft,drawableRight,drawableTop,drawableBottom。


根據業務的需要,要在代碼中設置控件的drawableLeft,drawableRight,drawableTop,drawableBottom屬性。

我們知道在xml中設置的方法為:android:drawableLeft="@drawable/xxxxx";

但是在代碼中並沒有相關的setDrawableLeft等方法。怎么辦呢?別擔心,api為我們提供了一個setCompoundDrawables(left,top,right,bottom);方法,供開發人員設置相應的邊界圖片。

操作方法十分簡單,如下代碼所示:

// 使用代碼設置drawableleft
        Drawable drawable = getResources().getDrawable(
                R.drawable.ic_qaa_top_icon);
        // / 這一步必須要做,否則不會顯示.
        drawable.setBounds(0, 0, drawable.getMinimumWidth(),
                drawable.getMinimumHeight());
        getTv_title().setCompoundDrawables(null, null, drawable, null);

ok,完成


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM