Android 中如何调节 TextView 的字间距
来源:互联网 发布:交互式屏幕互动软件 编辑:程序博客网 时间:2024/05/18 04:01
当前版本的 Android 似乎并未提供控制 TextView 的字间距方法。
搜索网上发现大量“教程”声称可以利用 TextView 的 setTextScaleX() 方法设置字间距,但从字面上(Scale)就可看出其实它是用于设置字体的缩放比率(试验结果亦是如此)。
最后从国外的一家论坛上发现一个解决方案:通过继承 TextView 并重写 setText() 和 getText() 方法,增加 setLetterSpacing() 等方法搞定该需求。
代码整理如下:
代码例——
/*** 示例:设置 TextView 的字间距* @author Pedro Barros (pedrobarros.dev at gmail.com)* @since May 7, 2013*/ import android.content.Context;import android.text.Spannable;import android.text.SpannableString;import android.text.style.ScaleXSpan;import android.util.AttributeSet;import android.widget.TextView; public class LetterSpacingTextView extends TextView { private float letterSpacing = LetterSpacing.NORMAL; private CharSequence originalText = ""; public LetterSpacingTextView(Context context) { super(context); } public LetterSpacingTextView(Context context, AttributeSet attrs){ super(context, attrs); } public LetterSpacingTextView(Context context, AttributeSet attrs, int defStyle){ super(context, attrs, defStyle); } public float getLetterSpacing() { return letterSpacing; } public void setLetterSpacing(float letterSpacing) { this.letterSpacing = letterSpacing; applyLetterSpacing(); } @Override public void setText(CharSequence text, BufferType type) { originalText = text; applyLetterSpacing(); } @Override public CharSequence getText() { return originalText; } private void applyLetterSpacing() { 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((letterSpacing+1)/10), i, i+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } super.setText(finalText, BufferType.SPANNABLE); } public class LetterSpacing { public final static float NORMAL = 0; }}
应用例——
LetterSpacingTextView textView = new LetterSpacingTextView(context);textView.setLetterSpacing(10); //参数为 float 类型。可另设其他值如 0 或者默认值 LetterSpacingTextView.LetterSpacing.NORMALtextView.setText("My text");//Add the textView in a layout, for instance:((LinearLayout) findViewById(R.id.myLinearLayout)).addView(textView);
参考自: <http://stackoverflow.com/questions/1640659/how-to-adjust-text-kerning-in-android-textview/1644061#1644061>
0 0
- Android 中如何调节 TextView 的字间距
- android textview设置字体的行距和字间距
- android textview设置字体的行距和字间距
- android textview设置字体的行距和字间距
- android textview设置字体的行距和字间距
- android textview设置字体的行距和字间距
- android textview设置字体的行距和字间距
- android textview设置字体的行距和字间距
- android textview设置字体的行距和字间距
- android-textview设置字体的行距和字间距
- android textview设置字体的行距和字间距
- android textview设置字体的行距和字间距
- android字间距实现,textview字间距实现
- Android中如何使用View填充在多个TextView之间使之等间距
- Android textView字间距自定义LetterSpacingTextView
- IEEE Conference模板中参考文献间距的调节
- TextView字间距问题
- 自定义TextView字间距
- 何谓补色
- 浅析设计模式之单例模式(又名:单值模式、单件模式)
- ORA-19809: 超出了恢复文件数的限制
- 刷题不解释~~~
- WordPress中修改指定文字的大小
- Android 中如何调节 TextView 的字间距
- 在cmd用zf.bat创建zend framework框架
- A == B?
- android中的startActivities
- 笔试基础知识复习(一)
- 创建视图的with check option选项。
- Tomcat下配置虚拟路径管理web项目
- 虚拟机中桥接,host-only和NAT连接方式的区别
- 流媒体服务器中的单播点播和广播