這個方法可以在TextView的四周加上一個Drawable圖標(biāo)。對于只知道TextView顯示文字的,是不是很高大上。
setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)
看代碼就知道,參數(shù)順序是左上右下。不想在四周加圖片的話,可以設(shè)置為null。
But,設(shè)置的Drawable有要求,必須要設(shè)置它的寬高,也就是要調(diào)用setBounds(int left, int top, int right, int bottom);其中需要給right和bottom設(shè)置固有寬度和固有高度,如果想設(shè)置drawable的寬高可以設(shè)置getIntrinsicWidth()和getIntrinsicHeight();
getIntrinsicWidth() /getIntrinsicHeight 獲取view的固定寬度和高度(單位是dp)
setCompoundDrawablesWithIntrinsicBounds( Drawable left, Drawable top, Drawable right, Drawable bottom)
這個方法比上邊的就簡便多了,等于將setBounds()這個方法合并到了setCompoundDrawablesWithIntrinsicBounds()的里邊,直接將drawable的寬高自動獲取。我們只需要將drawable參數(shù)賦值就可以了。對于不單獨(dú)設(shè)置寬高還是很簡便的。
兩種情況,各取所需。