真正改变字间距的方法

Android本身没提供真正改变字间距的方法

直接上代码

    public class MyTextView extends TextView {
    private String content;
    private int width;
    private Paint paint;
    private int textHeight;
    private int yPadding;
    int count;
    //记录每个字的二维数组
    int[][] position;

    private int color;
    private int size;
    private String s;
    private boolean mInitialized = false;

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

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

    public MyTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomTextView, defStyle, 0);
        int n = array.getIndexCount();
        for (int i=0; i<n; i++){
            int attr = array.getIndex(i);
            switch (attr){
                case R.styleable.CustomTextView_textColor:
                    color = array.getColor(attr, Color.BLUE);
                    break;

                case R.styleable.CustomTextView_textSize:
                    size = array.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics()));
                    break;

                case R.styleable.CustomTextView_text:
                    s = array.getString(attr);
                    break;
            }
        }
        array.recycle();
        paint = new Paint();
        paint.setColor(color);
        paint.setTypeface(Typeface.DEFAULT);
        paint.setTextSize(size);
        Paint.FontMetrics fm = paint.getFontMetrics();// 得到系统默认字体属性
        textHeight = (int) (Math.ceil(fm.descent - fm.ascent - size/4));// 获得字体高度
        yPadding = Utils.dipToPx(this.getContext(), 10f);

        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onGlobalLayout() {
                if (!mInitialized) {
                    mInitialized = true;
                    getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    width = getMeasuredWidth();
                    setText(s);
                }
            }
        });
    }

    public void setText(String str) {
        if (str == null)
            str = "";
        if (!mInitialized) {
            ViewGroup.LayoutParams params = getLayoutParams();
            width = params.width;
            mInitialized = true;
        }
        setPosition(str);
        //重新画控件
        this.invalidate();

    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (!TextUtils.isEmpty(content)) {
            for (int i = 0; i < count; i++) {
                canvas.drawText(String.valueOf(content.charAt(i)), position[i][0], position[i][1], paint);
            }
        }
    }

    public void setPosition(String content){
        this.content = content;
        count = content.length();
        int x = 0;
        char ch;
        position = new int[count][2];
        float textWith = 0f;
        for (int i=0; i<count; i++){
            ch = content.charAt(i);
            String str = String.valueOf(ch);
            Rect rect = new Rect();
            paint.getTextBounds(str, 0, 1, rect);
            int strwidth = rect.width();
            textWith = Math.max(strwidth, textWith);
        }

        for (int i=0; i<count; i++){
            if (i != 0){
                x += (width - count * textWith)/(count - 1);
            }
            position[i][0] = x;
            position[i][1] = textHeight;
            x += textWith;
            this.setHeight(textHeight + yPadding);
        }
    }
}
    <declare-styleable name="CustomTextView">
        <attr name="textColor" format="color" />
        <attr name="textSize" format="dimension" />
        <attr name="text" format="string" />
    </declare-styleable>

使用例子

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.gzfgeh.CustomTextView.MyTextView
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:id="@+id/text1"
        app:textSize="20px"
        app:textColor="@android:color/black"/>

    <com.gzfgeh.CustomTextView.MyTextView
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:id="@+id/text2"
        app:textSize="30px"
        app:textColor="@android:color/holo_blue_bright"/>

    <com.gzfgeh.CustomTextView.MyTextView
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:id="@+id/text3"
        app:textSize="30px"
        app:textColor="@android:color/holo_blue_bright"
        app:text="我非常好"/>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <com.gzfgeh.CustomTextView.MyTextView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:id="@+id/text4"
            app:textSize="30px"
            app:textColor="@android:color/holo_blue_bright"
            app:text="我非常好"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/text4"
            android:text="123456"
            android:textSize="36px"
            android:layout_toRightOf="@+id/text4"/>
    </RelativeLayout>

</LinearLayout>

效果图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值