一个TextView控件搞定图文一行布局

首先,图文一行的效果图如下:

一个TextView控件搞定图文一行布局_第1张图片

这里每一行只用了一个TextView即完成了布局,代码十分简单,主要是drawableLeft、drawableRight、drawableBottom、drawableTop属性的使用和drawablePadding的设置。具体代码如下:

"match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:drawablePadding="10dp"
        android:paddingTop="14dp"
        android:paddingLeft="18dp"
        android:paddingRight="8dp"
        android:background="#fff"
        android:drawableLeft="@mipmap/smile"
        android:drawableRight="@mipmap/next"
        android:drawableBottom="@mipmap/dividline"
        android:text="微笑"/>

还有一种效果则是让drawbleLeft与文本一起居中显示,但设置gravity为center也没有用,于是自定义TextView控件来实现居中效果。代码如下:

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;

/**
 * 自定义TextView,让Drawable与文本一起居中显示
 */
public class DrawCenterTextView extends AppCompatTextView {

    public DrawCenterTextView(Context context) {
        this(context, null);
    }

    public DrawCenterTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DrawCenterTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        Drawable[] drawables = getCompoundDrawables();

        float textWidth = getPaint().measureText(getText().toString());
        double textHeight = getLineHeight() * getLineCount();

        int totalDrawablePaddingH = 0; //水平方向的padding填充
        int totalDrawablePaddingV = 0; //垂直方向的padding填充
        int drawableWidth = 0;
        int drawableHeight = 0;
        float totalWidth;
        float totalHeight;
        int paddingH;
        int paddingV;

        if (drawables != null){
            Drawable drawableLeft = drawables[0],
                    drawableTop = drawables[1],
                    drawableRight = drawables[2],
                    drawableBottom = drawables[3];

            // measure width
            if (drawableLeft != null){
                drawableWidth += drawableLeft.getIntrinsicWidth();
                totalDrawablePaddingH += getCompoundDrawablePadding();
            }
            if (drawableRight != null){
                drawableWidth += drawableRight.getIntrinsicWidth();
                totalDrawablePaddingH += getCompoundDrawablePadding();
            }
            totalWidth = textWidth + drawableWidth + totalDrawablePaddingH;
            paddingH = (int) (getWidth() - totalWidth) / 2;

            // measure height
            if (drawableTop != null){
                drawableHeight += drawableTop.getIntrinsicHeight();
                totalDrawablePaddingV += getCompoundDrawablePadding();
            }
            if (drawableBottom != null){
                drawableHeight += drawableBottom.getIntrinsicHeight();
                totalDrawablePaddingV += getCompoundDrawablePadding();
            }
            totalHeight = (float) (textHeight + drawableHeight + totalDrawablePaddingV);
            paddingV = (int) (getHeight() - totalHeight) / 2;

            setPadding(paddingH, paddingV, paddingH, paddingV);
        }

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

和普通TextView使用方法一样:

<com.nandity.mylibrary.DrawCenterTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:background="#fff"
        android:paddingTop="24dp"
        android:drawableLeft="@mipmap/happy"
        android:drawablePadding="10dp"
        android:text="很开心很开心"/>

实现的效果则如下所示:

一个TextView控件搞定图文一行布局_第2张图片

你可能感兴趣的:(Android--随手记)