Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

怎么设置图标离文字间的距离 #47

Open
CCTV-11 opened this issue Nov 16, 2017 · 5 comments
Open

怎么设置图标离文字间的距离 #47

CCTV-11 opened this issue Nov 16, 2017 · 5 comments

Comments

@CCTV-11
Copy link

CCTV-11 commented Nov 16, 2017

图标离文字实在是太远了,有没有动态设置距离的方式

@igding
Copy link

igding commented Jul 2, 2018

同问

@mosentest
Copy link

package com.yyydjk.library;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.widget.TextView;

/**
 * 作者 create by moziqi on 2018/7/12
 * 邮箱 709847739@qq.com
 * 说明
 * 参考这修改
 * https://blog.csdn.net/qq_35532429/article/details/52939740
 * https://blog.csdn.net/catoop/article/details/23947345
 **/
public class RightDrawableTextView extends TextView {

    private int mW, mH;

    public RightDrawableTextView(Context context) {
        super(context);
    }

    public RightDrawableTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public RightDrawableTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mW = getWidth();
        mH = getHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Drawable[] drawables = getCompoundDrawables();
        if (drawables != null) {
            //获取右边图片
            Drawable drawableRight = drawables[2];
            if (drawableRight != null) {
                //获取文字占用长宽
                String txtContent = getText().toString();
                int textWidth = (int) getPaint().measureText(txtContent);
                int textHeight = (int) getPaint().getTextSize();
                //获取图片实际长宽
                int drawableWidth = drawableRight.getIntrinsicWidth();
                int drawableHeight = drawableRight.getIntrinsicHeight();

                //setBounds修改Drawable在View所占的位置和大小,对应参数同样的 左上右下()
                if (txtContent != null) {
                    if (txtContent.length() == 2) {
                        if (drawableHeight < textHeight) {
                            drawableRight.setBounds(((textWidth - mW) / 2) + textWidth,
                                    0,
                                    ((textWidth - mW) / 2) + textWidth + drawableWidth,
                                    drawableHeight);
                        } else {
                            drawableRight.setBounds(((textWidth - mW) / 2) + textWidth,
                                    (textHeight - drawableHeight) / 2,
                                    ((textWidth - mW) / 2) + textWidth + drawableWidth,
                                    (textHeight - drawableHeight) / 2 + drawableHeight);
                        }

                    } else if (txtContent.length() == 4) {
                        if (drawableHeight < textHeight) {
                            drawableRight.setBounds(((textWidth - mW) / 2) + textWidth / 2,
                                    0,
                                    ((textWidth - mW) / 2) + textWidth / 2 + drawableWidth, drawableHeight);
                        } else {
                            drawableRight.setBounds(((textWidth - mW) / 2) + textWidth / 2,
                                    (textHeight - drawableHeight) / 2,
                                    ((textWidth - mW) / 2) + textWidth / 2 + drawableWidth,
                                    (textHeight - drawableHeight) / 2 + drawableHeight);
                        }
                    }
                }
            }
        }
        super.onDraw(canvas);
    }
}

@mosentest
Copy link

更换原本的TextView

@BigBigDeBoy
Copy link

@moz1q1 你是把源码导进来的,不是compile依赖的吗

@mosentest
Copy link

@BigBigDeBoy 是的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants