想實現文本加圖片效果,可以使用drawab屬性可以實現
代碼實現:
drawable = getResources().getDrawable(R.mipmap.ic_launcher);
/*這一步必須要做,否則不會顯示./
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());//對圖片進行壓縮
/*設置圖片位置,四個參數分別方位是左上右下,都設置爲null就表示不顯示圖片/
edSearch.setCompoundDrawables(drawable, null, drawable, null);
Xml實現:
android:drawableLeft=”@mipmap/ic_launcher”
android:drawablePadding=”@dimen/margin_8”
android:drawableRight=”@mipmap/ic_launcher”
Drawable新屬性:
edSearch.setCompoundDrawablesWithIntrinsicBounds(drawable, null, drawable, null);
可以替換
drawable = getResources().getDrawable(R.mipmap.ic_launcher);
/**這一步必須要做,否則不會顯示.*/
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());//對圖片進行壓縮
/**設置圖片位置,四個參數分別方位是左上右下,都設置爲null就表示不顯示圖片*/
edSearch.setCompoundDrawables(drawable, null, drawable, null);