看到这个问题,我查看了下网上有很多说设置

textView有一个属性android:textScaleX是调节字间距的,它的值是一个float型。查看源代码,默认textView 此属性是使用的是:

android.internal.R.styleable.TextView_textScaleX

setTextScaleX(a.getFloat(attr, 1.0f));

但是这个显示的效果只是拉伸,并没有改变字体间的间距

根据Android api解释:


android:textScaleX     setTextScaleX(float)    Sets the horizontal scaling factor for the text. 

只是设置了文本的水平缩放,

android:letterSpacing  setLetterSpacing(float)   Text letter-spacing. 

这个是设置文本缩放,但是只有在API>=21,才能使用


所以重写textview;


public class LetterSpacingTextView extends TextView {    private float spacing = Spacing.NORMAL;    private CharSequence originalText = "";    public LetterSpacingTextView(Context context) {        super(context);    }    public LetterSpacingTextView(Context context, AttributeSet attrs) {        super(context, attrs);        init(context, attrs);    }    public LetterSpacingTextView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        init(context, attrs);    }    public float getSpacing() {        return this.spacing;    }    private void init(Context context, AttributeSet attrs) {        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.LetterSpacingTextView);        originalText = array.getString(R.styleable.LetterSpacingTextView_text);        setSpacing(array.getFloat(R.styleable.LetterSpacingTextView_textSpacing, 0));        array.recycle();    }    public void setSpacing(float spacing) {        this.spacing = spacing;        applySpacing();    }    @Override    public void setText(CharSequence text, BufferType type) {        originalText = text;        applySpacing();    }    @Override    public CharSequence getText() {        return originalText;    }    private void applySpacing() {        if (this == null || this.originalText == null) return;        StringBuilder builder = new StringBuilder();        for (int i = 0; i < originalText.length(); i++) {            builder.append(originalText.charAt(i));            if (i + 1 < originalText.length()) {                builder.append("\u00A0");            }        }        SpannableString finalText = new SpannableString(builder.toString());        if (builder.toString().length() > 1) {            for (int i = 1; i < builder.toString().length(); i += 2) {                finalText.setSpan(new ScaleXSpan((spacing + 1) / 10), i, i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);            }        }        super.setText(finalText, BufferType.SPANNABLE);    }    public class Spacing {        public final static float NORMAL = 0;    }}


attrs.xml

<?xml version="1.0" encoding="utf-8"?>                        
xml中使用示例:

<?xml version="1.0" encoding="utf-8"?>    

Java代码中使用示例:

 LetterSpacingTextView textView = (LetterSpacingTextView) findViewById(R.id.textView);        textView.setText("出自宋代诗人李清照的《鹧鸪天·桂花》");        textView.setSpacing(5);
改编自:How to adjust text kerning in Android TextView? 

查看:http://blog.csdn.net/aikongmeng/article/details/50553806
增加了xml配置字间距的属性




更多相关文章

  1. 上中下布局,上下高度指定,中间自适应(左中右同理)
  2. Android(安卓)属性设置失败
  3. Android悬浮窗口基本知识
  4. 动画效果编程基础
  5. AndroidManifest.xml 标签含义字典
  6. android默认关闭Location服务及关闭锁屏功能
  7. 初学Android,图形图像之Matrix(二十九)
  8. Android(安卓)Zxing创建带文字的一维码(条形码)
  9. android:configChanges 属性注意事项 翻转不起作用的问题

随机推荐

  1. Android(安卓)根文件系统启动过程
  2. Android(安卓)ICS关机跟重新启动功能研究
  3. android logcat详解及缓存
  4. Android Develop Tips
  5. Android批量图片加载经典系列——Volley
  6. Android(安卓)Databinding技术由浅入深(4)-
  7. Android 中 Button 的基本使用
  8. android:roundIcon和android:icon的区别
  9. Android整合网上资源以及个人对GreenDao
  10. Android内存分析工具