Android textView字间距自定义LetterSpacingTextView
LetterSpacingTextView.java
package com.akm.akmviewpager;/*** Created by toge on 16/1/20.* 字间距*/import android.content.Context;
import android.content.res.TypedArray;
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 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();}@Overridepublic void setText(CharSequence text, BufferType type) {originalText = text;applySpacing();}@Overridepublic 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"?>
<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);
改编自:How to adjust text kerning in Android TextView?
增加了xml配置字间距的属性
Android textView字间距自定义LetterSpacingTextView相关推荐
- Android Study 之真正解决TextView字间距,那些扯淡的边儿去吧
LZ-Says:从个人的角度来说为什么要写博客,一方面可以拓展知识,通过分享去了解更多的知识:二方面可以通过优质的博文去推广自己. 写博客,就好像工作一样,要认真,要对自己写的东西负责,更要对别人看的 ...
- 自定义TextView字间距
前言 说说今天遇到的问题吧. 老大说,搞个类似身份证原样的布局.其中一些TextView需要设置相对应的字间距,网上搜了n个,郁闷的我,真想骂娘,都是什么啊.不过,民间总有大神在,好歹解决了我的问题. ...
- Android TextView 字符间距设置
需要动态设置textview字符间距,搜了很多都说是android:textScaleX可以设置文字间距,实际这属性是设置文字横向缩放的. 有一些通过String.xml或者html的方法可以实现,但 ...
- Android设置字间距和行间距
设置字间距用: android:letterSpacing eg: android:letterSpacing = "1.2" 其相关方法是 setLetterSpacing(fl ...
- android textview字体贴底部,在android中底部设置textview
我在Linearlayout里面有Textview.我希望它显示在底部,但它只显示在我的中心.我的xml文件看起来像下面的代码.我尝试用RelativeLayout替换LinearLayout但它没有 ...
- android+设置字体行高,TextView设置行间距、行高,以及字间距
一. 设置TextView行间距.行高: Android系统中TextView有默认行间距,但是比较窄有的时候需要我们设置每行行间距. TextView为我们提供了相关设置属性android:line ...
- android字体行距,android textview设置字体的行距和字间距
字间距 textView有一个属性android:textScaleX是调节字间距的,它的值是一个float型.查看源代码,默认textView 此属性是使用的是: android.internal. ...
- android+设置字体行高,android textview设置字体的行距和字间距
android textview设置字体的行距和字间距 字间距 textView有一个属性android:textScaleX是调节字间距的,它的值是一个float型.查看源代码,默认textView ...
- Android基础篇 TextView 设置行间距字间距
一.行间距 TextView 设置行间距可以通过设置TextView属性的 android:lineSpacingExtra 或 android:lineSpacingMultiplier 来达到效果 ...
最新文章
- Excel电子表格的使用技巧!很实用
- NSIS 打包文件添加防火墙白名单
- KNN 算法--图像分类算法
- uml 类图_UML-类图
- leetcode23-合并K个升序链表
- Ubuntu 平滑升级到PHP7
- AOP下的权限控制实现
- 罗永浩:6亿债务,已还了4个亿;Python 3.9指日可待|极客头条
- php debug pit,start.php
- 地图学(何宗宜版)知识点整理
- 学习笔记 Tianmao 篇 FragmentTabHost (TabHost升级版)
- OpenSees二次开发实例01
- python PyEnchant(检查拼写)
- creator物理引擎 /RPG运动类型 /坦克
- 【WinForm】关于截图识别数字并计算的桌面程序实现方案
- 计算机英语教程第二版课文朗读,英语口语教程
- ATT的SIM卡(GoPhone)连不上4G网络怎么办
- 今日科技联播:蚂蚁金服回应支付宝法人变更,子弹短信总激活用户超过400万...
- Javascript 从ES5到ES7 - 14 类型运算
- 知网专利信息爬虫!强无敌!
热门文章
- linux 复制硬盘无法启动,linux – 用克隆替换硬盘失败
- 2022-2028中国小型核磁共振波谱仪(NMR)市场现状研究分析与发展前景预测报告
- 中小企业应该如何实施软文营销
- backtrader plot 出现错误的处理办法 ImportError: cannot import name ‘warnings‘ from ‘matplotlib.dates‘
- mysql数据什么格式_Mysql数据格式
- Python实现Collatz序列(考拉兹猜想)
- 多特征融合的高分辨率遥感图像海陆分离——刘思彤
- 关于TED talks 字幕下载的方法
- CMake编译Mitsuba
- 【Unity】第三人称射击游戏开发过程之瞄准状态设计(TPS.S.P1)