自定义TextView,去掉没有文字时的空白区域

来源:互联网 发布:中箭mod 知乎 编辑:程序博客网 时间:2024/05/21 04:18

1.需求

文字和icon的布局采用TextView的setCompoundDrawables()来实现。

2.遇到的问题

垂直布局,icon在上面,文字在下面,当没有文字的时候,下方文字区域会有一片空白。

3.解决办法

在onMeasure()中做手脚,当文字为空时,我们自己手动设计宽高。关键代码:
@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    if (getText().length() == 0) {        int widthMode = MeasureSpec.getMode(widthMeasureSpec);        int heightMode = MeasureSpec.getMode(heightMeasureSpec);        int widthSize = MeasureSpec.getSize(widthMeasureSpec);        int heightSize = MeasureSpec.getSize(heightMeasureSpec);        int w = iconWidth;        int h = iconHeight;        if (widthMode == MeasureSpec.EXACTLY) {            w = widthSize;        }        if (heightMode == MeasureSpec.EXACTLY) {            h = heightSize;        }        setMeasuredDimension(w, h);    } else {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);    }}

4.完整代码

class IconTextView extends TextView {    public static final int LEFT = 0, TOP = 1, RIGHT = 2, BOTTOM = 3;    int iconWidth;    int iconHeight;    int iconOrientation = TOP;    public IconTextView(Context context) {        super(context);        setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));        setGravity(Gravity.CENTER);        setCompoundDrawablePadding(0);    }    public void setIcon(int resId) {        if (resId > 0) {            final Resources resources = getContext().getResources();            Drawable icon = resources.getDrawable(resId);            int width = iconWidth == 0 ? icon.getIntrinsicWidth() : iconWidth;            int height = iconHeight == 0 ? icon.getIntrinsicHeight() : iconHeight;            icon.setBounds(0, 0, width, height);            switch (iconOrientation) {                case LEFT:                    setCompoundDrawables(icon, null, null, null);                    break;                case TOP:                    setCompoundDrawables(null, icon, null, null);                    break;                case RIGHT:                    setCompoundDrawables(null, null, icon, null);                    break;                case BOTTOM:                    setCompoundDrawables(null, null, null, icon);                    break;            }        }    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        if (getText().length() == 0) {            int widthMode = MeasureSpec.getMode(widthMeasureSpec);            int heightMode = MeasureSpec.getMode(heightMeasureSpec);            int widthSize = MeasureSpec.getSize(widthMeasureSpec);            int heightSize = MeasureSpec.getSize(heightMeasureSpec);            int w = iconWidth;            int h = iconHeight;            if (widthMode == MeasureSpec.EXACTLY) {                w = widthSize;            }            if (heightMode == MeasureSpec.EXACTLY) {                h = heightSize;            }            setMeasuredDimension(w, h);        } else {            super.onMeasure(widthMeasureSpec, heightMeasureSpec);        }    }}

                        2015年12月18日21:26:32
0 0