項(xiàng)目中經(jīng)常需要設(shè)置 drawable 大小,但是可惜 textView 并不支持
手摸手,三分鐘動(dòng)手造輪子:
支持 任意 Drawable,不限于圖片, XML,layer,再也不怕產(chǎn)品的各種奇葩需求了
- 創(chuàng)建自定義的 View
public class DrawableTextView extends AppCompatTextView {
//設(shè)置方向
public static final int LEFT = 1, TOP = 2, RIGHT = 3, BOTTOM = 4;
public DrawableTextView(Context context) {
this(context, null);
}
public DrawableTextView(Context context, AttributeSet attrs){
this(context, attrs, 0);
}
public DrawableTextView(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs);
}
}
- 在
attrs.xml
設(shè)置自定義屬性:
<declare-styleable name="DrawableTextView">
<attr name="drawable_src" format="reference"/>
<attr name="drawable_height" format="dimension"/>
<attr name="drawable_width" format="dimension"/>
<attr name="drawable_location">
<enum name="left" value="1"/>
<enum name="top" value="2"/>
<enum name="right" value="3"/>
<enum name="bottom" value="4"/>
</attr>
</declare-styleable>
- 解析自定義屬性
private void init(AttributeSet attrs) {
if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.DrawableTextView);
int mWidth = a.getDimensionPixelSize
(R.styleable.DrawableTextView_drawable_width, 0);
int mHeight =a.getDimensionPixelSize
(R.styleable.DrawableTextView_drawable_height,0);
Drawable mDrawable =a.getDrawable
(R.styleable.DrawableTextView_drawable_src);
int mLocation = a.getInt
(R.styleable.DrawableTextView_drawable_location, LEFT);
a.recycle();
drawDrawable(mDrawable, mWidth, mHeight, mLocation);
}
}
-
給 Drawable 設(shè)置好 參數(shù)
/** * 繪制Drawable寬高,位置 */ public void drawDrawable(Drawable mDrawable, int mWidth, int mHeight, int mLocation) { if (mDrawable != null) { if (mWidth != 0 && mHeight != 0) { mDrawable.setBounds(0, 0, mWidth, mHeight); } switch (mLocation) { case LEFT: this.setCompoundDrawables(mDrawable, null, null, null); break; case TOP: this.setCompoundDrawables(null, mDrawable, null, null); break; case RIGHT: this.setCompoundDrawables(null, null, mDrawable, null); break; case BOTTOM: this.setCompoundDrawables(null, null, null, mDrawable); break; } } }
-
最后,使用:
<com.mrwang.gifstudio.View.DrawableTextView android:id="@+id/view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="hahahah" app:drawable_src="@drawable/count_down" android:drawablePadding="10dp" app:drawable_height="50dp" app:drawable_width="50dp" app:drawable_location="right"/>
?
貼上最終效果圖:
-
所有代碼:
public class DrawableTextView extends AppCompatTextView { public static final int LEFT = 1, TOP = 2, RIGHT = 3, BOTTOM = 4; public DrawableTextView(Context context) { this(context, null); } public DrawableTextView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public DrawableTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(attrs); } private void init(AttributeSet attrs) { if (attrs != null) { TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.DrawableTextView); int mWidth = a .getDimensionPixelSize(R.styleable.DrawableTextView_drawable_width, 0); int mHeight = a.getDimensionPixelSize(R.styleable.DrawableTextView_drawable_height, 0); Drawable mDrawable = a.getDrawable(R.styleable.DrawableTextView_drawable_src); int mLocation = a.getInt(R.styleable.DrawableTextView_drawable_location, LEFT); a.recycle(); drawDrawable(mDrawable, mWidth, mHeight, mLocation); } } /** * 繪制Drawable寬高,位置 */ public void drawDrawable(Drawable mDrawable, int mWidth, int mHeight, int mLocation) { if (mDrawable != null) { if (mWidth != 0 && mHeight != 0) { mDrawable.setBounds(0, 0, mWidth, mHeight); } switch (mLocation) { case LEFT: this.setCompoundDrawables(mDrawable, null, null, null); break; case TOP: this.setCompoundDrawables(null, mDrawable, null, null); break; case RIGHT: this.setCompoundDrawables(null, null, mDrawable, null); break; case BOTTOM: this.setCompoundDrawables(null, null, null, mDrawable); break; } } } }
?參考:
| http://blog.csdn.net/u014702653/article/details/52304656