TextView,Button的DrawableLeft,,Right 使用代碼動態的設置

Xml文件中設置相信大家都已經非常的熟悉了.

            <TextView
                android:id="@+id/register_zone"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:drawableRight="@mipmap/lbs_arrow_down"/>

使用代碼設置DrawableLeft,Right

第一種方案

       Drawable drawable = SwmUIUtils.getDrawable(imgs[position]);
                    // 確定drawable的位置
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//                      setCompoundDrawables 畫的drawable的寬高是按drawable.setBound()設置的寬高,
//                    所以才有The Drawables must already have had setBounds(Rect) called.
//                    使用之前必須使用Drawable.setBounds設置Drawable的長寬。`

                    mTaskProgress.setCompoundDrawables(null, null, drawable, null);

第二種方案

setCompoundDrawablesWithIntrinsicBounds(null, null, UIUtils.getDrawable(R.mipmap.up_arrow), null);

setCompoundDrawablesWithIntrinsicBounds是畫的drawable的寬高是按drawable固定的寬高,
所以才有The Drawables' bounds will be set to their intrinsic bounds.

即通過getIntrinsicWidth()與getIntrinsicHeight()獲得,
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 一. Java基礎部分.................................................
    wy_sure閱讀 3,834評論 0 11
  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法,內部類的語法,繼承相關的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,759評論 18 399
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,124評論 25 708
  • 217. Contains Duplicate 這是leetCode第217題 題目 Given an array...
    打鐵大師閱讀 288評論 0 1
  • 漫漫人生路 ,已經二十一的人了,現在大三的生活甚是迷茫,馬上面臨畢業,我將何去何從,又不甘心碌碌無為,更不甘心平庸...
    懂得憐憫閱讀 318評論 0 0