一般來(lái)說(shuō)TextView側(cè)邊的圖標(biāo)位置都是居中顯示的,并且圖標(biāo)大小是按照?qǐng)D標(biāo)的尺寸來(lái)顯示的捂寿。例如叉橱,假如你給TextView設(shè)置了drawableLeft,此時(shí)該圖標(biāo)的位置以及大小是不可控的者蠕,這樣就會(huì)出現(xiàn)兩種問(wèn)題:
1窃祝、如果TextView文字過(guò)長(zhǎng)導(dǎo)致?lián)Q行,然而左邊的圖標(biāo)仍然是按照TextView的高度垂直居中顯示踱侣,然而我們想要的卻是想讓圖標(biāo)在第一行居中顯示授嘀;
2、圖標(biāo)過(guò)大或者過(guò)小不可控怨绣;
此時(shí)就要考慮自定義TextView來(lái)解決此類(lèi)問(wèn)題了赋秀。具體思路為:
1、對(duì)于圖標(biāo)的位置待榔,可以考慮設(shè)置圖標(biāo)Drawable的整體偏移量逞壁;
2、對(duì)于圖標(biāo)的大小锐锣,則是自定義其大须绱场;
具體顯示效果如下:
圖標(biāo)方位及大小.png
具體實(shí)現(xiàn)如下:
public class DrawableTextView extends AppCompatTextView {
// 控件左雕憔、上姿骏、右、下圖標(biāo)的寬和高
private int drawableLeftWidth, drawableTopWidth, drawableRightWidth, drawableBottomWidth;
private int drawableLeftHeight, drawableTopHeight, drawableRightHeight, drawableBottomHeight;
// 默認(rèn)顯示方式
private boolean alignCenter = true;
private int mWidth;
public DrawableTextView(Context context) {
this(context, null);
}
public DrawableTextView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public DrawableTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context, attrs);
}
private void initView(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DrawableTextView);
drawableLeftWidth = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_drawableLeftWidth, 0);
drawableTopWidth = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_drawableTopWidth, 0);
drawableRightWidth = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_drawableRightWidth, 0);
drawableBottomWidth = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_drawableBottomWidth, 0);
drawableLeftHeight = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_drawableLeftHeight, 0);
drawableTopHeight = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_drawableTopHeight, 0);
drawableRightHeight = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_drawableRightHeight, 0);
drawableBottomHeight = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_drawableBottomHeight, 0);
alignCenter = typedArray.getBoolean(R.styleable.DrawableTextView_alignCenter, true);
typedArray.recycle();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mWidth = w;
Drawable[] drawables = getCompoundDrawables();
Drawable drawableLeft = drawables[0];
Drawable drawableTop = drawables[1];
Drawable drawableRight = drawables[2];
Drawable drawableBottom = drawables[3];
if (drawableLeft != null) {
setDrawable(drawableLeft, 0, drawableLeftWidth, drawableLeftHeight);
}
if (drawableTop != null) {
setDrawable(drawableTop, 1, drawableTopWidth, drawableTopHeight);
}
if (drawableRight != null) {
setDrawable(drawableRight, 2, drawableRightWidth, drawableRightHeight);
}
if (drawableBottom != null) {
setDrawable(drawableBottom, 3, drawableBottomWidth, drawableBottomHeight);
}
this.setCompoundDrawables(drawableLeft, drawableTop, drawableRight, drawableBottom);
}
/**
* 設(shè)置圖標(biāo)大小及其偏移量
*/
private void setDrawable(Drawable drawable, int tag, int drawableWidth, int drawableHeight) {
int width = drawableWidth == 0 ? drawable.getIntrinsicWidth() : drawableWidth;
int height = drawableHeight == 0 ? drawable.getIntrinsicHeight() : drawableHeight;
int left = 0, top = 0, right = 0, bottom = 0;
switch (tag) {
case 0:
case 2:
left = 0;
top = alignCenter ? 0 : -getLineCount() * getLineHeight() / 2 + getLineHeight() / 2;
right = width;
bottom = top + height;
break;
case 1:
case 3:
left = alignCenter ? 0 : -mWidth / 2 + width / 2;
top = 0;
right = left + width;
bottom = top + height;
break;
}
drawable.setBounds(left, top, right, bottom);
}
}