Drawable居中的TextView(Button、RadioButton)

一、概述

TextView 以及繼承自 TextView 的 Widget 都是同樣的,設(shè)置 DrawableTop(Left/Right/Bottom),都會對其 TextView 的邊緣,而大多數(shù)情況我們需要的是顯示在中間位置。
參照網(wǎng)上的 DrawableCenterTextView 進行改進

二、字體高度說明

在 TextView 里面的字體高度是一個自定義View 經(jīng)常會遇到的問題,在這里稍微研究一下:
字體高度通過 Paint.FontMetrics 獲取,下面是跟字體高度有關(guān)的幾個屬性

  • baseline 基線,以下所有屬性都是根據(jù) baseline 而來的

自定義一個 TextView,在 onDraw 將所有線都通過 DrawLine 畫出來,代碼如下:

    Paint mPaint = new Paint();
 
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mPaint.setColor(Color.RED);
        mPaint.setTextSize(sp2px(8));

        Paint.FontMetrics fontMetrics = getPaint().getFontMetrics();
        //baseline
        canvas.drawLine(0, getBaseline(), getWidth(), getBaseline(), mPaint);
        canvas.drawText("baseline", 0, getBaseline(), mPaint);
        //top
        canvas.drawLine(0, fontMetrics.top + getBaseline(), getWidth(), fontMetrics.top + getBaseline(), mPaint);
        canvas.drawText("top", getWidth() / 5, fontMetrics.top + getBaseline(), mPaint);
        //ascent
        canvas.drawLine(0, fontMetrics.ascent + getBaseline(), getWidth(), fontMetrics.ascent + getBaseline(), mPaint);
        canvas.drawText("ascent", getWidth() * 2 / 5, fontMetrics.ascent + getBaseline(), mPaint);
        //descent
        canvas.drawLine(0, fontMetrics.descent + getBaseline(), getWidth(), fontMetrics.descent + getBaseline(), mPaint);
        canvas.drawText("descent", getWidth() * 3 / 5, fontMetrics.descent + getBaseline(), mPaint);
        //bottom
        canvas.drawLine(0, fontMetrics.bottom + getBaseline(), getWidth(), fontMetrics.bottom + getBaseline(), mPaint);
        canvas.drawText("bottom", getWidth() * 4 / 5, fontMetrics.bottom + getBaseline(), mPaint);
        //leading
        canvas.drawLine(0, fontMetrics.leading + getBaseline(), getWidth(), fontMetrics.leading + getBaseline(), mPaint);
        canvas.drawText("leading", getWidth(), fontMetrics.leading + getBaseline(), mPaint);
    }

運行的結(jié)果如下:

在這幾個值中,baseline 代表0,ascent 為正值,descent 為負值
通用的字體高度:ascent - descent
只需數(shù)字的字體高低:ascent + descent

三、 顯示效果

在了解字體高度后,通過 getCompoundDrawables(),獲得 Drawable 集合,可以得到 drawable 的高和寬進行操作,效果及代碼如下:

public class DrawableCenterTextView extends android.support.v7.widget.AppCompatTextView {
    /** 圖片列表 */
    private Drawable[] drawables;

    /**
     * 構(gòu)造函數(shù)
     *
     * @param context 上下文
     */
    public DrawableCenterTextView(Context context) {
        super(context);
    }

    /**
     * 構(gòu)造函數(shù)
     *
     * @param context 上下文
     * @param attrs 屬性
     */
    public DrawableCenterTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 構(gòu)造函數(shù)
     *
     * @param context 上下文
     * @param attrs 屬性
     * @param defStyle 樣式
     */
    public DrawableCenterTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        drawables = getCompoundDrawables();
        if (drawables[0] != null || drawables[2] != null) {
            // 左、右
            setGravity(Gravity.CENTER_VERTICAL | (drawables[0] != null ? Gravity.START : Gravity.END));
        } else if (drawables[1] != null || drawables[3] != null) {
            // 上、下
            setGravity(Gravity.CENTER_HORIZONTAL | (drawables[1] != null ? Gravity.TOP : Gravity.BOTTOM));
        }

        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int drawablePadding = getCompoundDrawablePadding();
        if (drawables[0] != null) {
            // 左
            int drawableWidth = drawables[0].getIntrinsicWidth();
            float bodyWidth;
            if (TextUtils.isEmpty(getText())) {
                bodyWidth = drawableWidth;
            } else {
                float textWidth = getPaint().measureText(getText().toString());
                bodyWidth = textWidth + drawableWidth + drawablePadding;
            }
            canvas.translate((getWidth() - bodyWidth) / 2, 0);

        } else if (drawables[2] != null) {
            // 右
            int drawableWidth = drawables[2].getIntrinsicWidth();
            float bodyWidth;
            if (TextUtils.isEmpty(getText())) {
                bodyWidth = drawableWidth;
            } else {
                float textWidth = getPaint().measureText(getText().toString());
                bodyWidth = textWidth + drawableWidth + drawablePadding;
            }
            canvas.translate((bodyWidth - getWidth()) / 2, 0);

        } else if (drawables[1] != null) {
            // 上
            int drawableHeight = drawables[1].getIntrinsicHeight();
            float bodyHeight;
            if (TextUtils.isEmpty(getText())) {
                bodyHeight = drawableHeight;
            } else {
                Paint.FontMetrics fm = getPaint().getFontMetrics();
                float fontHeight = (float) Math.ceil(fm.descent - fm.ascent);
                bodyHeight = fontHeight + drawableHeight + drawablePadding;
            }
            canvas.translate(0, (getHeight() - bodyHeight) / 2);

        } else if (drawables[3] != null) {
            // 下
            int drawableHeight = drawables[3].getIntrinsicHeight();
            float bodyHeight;
            if (TextUtils.isEmpty(getText())) {
                bodyHeight = drawableHeight;
            } else {
                Paint.FontMetrics fm = getPaint().getFontMetrics();
                float fontHeight = (float) Math.ceil(fm.descent - fm.ascent);
                bodyHeight = fontHeight + drawableHeight + drawablePadding;
            }
            canvas.translate(0, (bodyHeight - getHeight()) / 2);
        }
        super.onDraw(canvas);
    }
}

轉(zhuǎn)載注意出處:http://www.itdecent.cn/p/f4b6374d431f

Github 地址為,https://github.com/lwcye/DrawCenterTextView
如果覺得喜歡就star一下

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容