Android设置textview的字体之间的间距
来源:互联网 发布:苹果版手机淘宝 编辑:程序博客网 时间:2024/05/01 18:41
看到这个问题,我查看了下网上有很多说设置
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; }}
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="LetterSpacingTextView"> <attr name="textSpacing" format="float"/> <attr name="text" format="string"/> </declare-styleable></resources>xml中使用示例:
<?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:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.akm.akmviewpager.LetterSpacingTextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="match_parent" app:text="暗淡轻黄体性柔,情疏迹远只香留。 何须浅碧轻红色,自是花中第一流。梅定妒,菊应羞,画栏开处冠中秋。 骚人可煞无情思,何事当年不见收。" app:textSpacing="5" /></LinearLayout>
Java代码中使用示例:
LetterSpacingTextView textView = (LetterSpacingTextView) findViewById(R.id.textView); textView.setText("出自宋代诗人李清照的《鹧鸪天·桂花》"); textView.setSpacing(5);</span>改编自:How to adjust text kerning in Android TextView?
查看:http://blog.csdn.net/aikongmeng/article/details/50553806
增加了xml配置字间距的属性
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设置字体的行距和字间距
- 关于给textview设置字体间的间距
- android-textView-字体的设置
- Android TextView字体的设置
- android textView调整字体的间距和行间距
- 字体之间的间距调整
- textview字符之间的间距(letterspacing)
- CMarkup 入门(增删改查)
- HBase数据备份
- [高斯消元与线性基]
- MySQL 5.7 root登录问题
- VM的几种网络连接方式
- Android设置textview的字体之间的间距
- IIC总线协议总结
- js中如何定义常量
- 更换CentOS的下载源为阿里源
- 计算机视觉中的ICP算法
- 从开闭原则,讲讲初级的代码设计
- iOS开发中@property的属性weak nonatomic strong readonly等介绍
- Mac系统硬盘复制之后启动慢的问题
- 使用Chrome DevTools的Timeline和Profiles提高Web应用程序的性能