真正改变字间距的方法
来源:互联网 发布:双色球算法 编辑:程序博客网 时间:2024/05/16 08:02
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
- 真正改变字间距的方法
- 真正解决TextView行间距、字间距的问题
- 真正解决TextView行间距、字间距的问题
- 真正解决TextView行间距、字间距的问题
- 改变list列表的间距
- 真正的改变
- 表格 改变分组列表之间的间距
- 改变UITextView文字之间的间距
- Android Study 之真正解决TextView字间距,那些扯淡的边儿去吧
- 今天学习了词间距、超出隐藏、字间距、margin使用和塌陷方法的解决
- 设置间距的简便方法
- iOS开发小知识之改变UIlabel的行间距和字间距
- iOS开发小知识之改变UIlabel的行间距和字间距
- 自定义UILabel设置行距和字间距的各种方法
- 改变字符之间的间距 英文和汉字
- 真正的改变,来自于三句话
- Docker 1.12:真正的游戏改变者
- UILabel改变行间距、字间距(swift)
- nginx文件类型错误解析漏洞
- cocos2dx lua 3.10 接入cjson
- 剑指offer 数字在排序数组中出现的次数
- 判断android设备是否root
- 添加OpenCV_contrib库至OpenCV3.1.0中(Windows 64位 环境下编译OpenCV3.1.0和OpenCV_contrib)
- 真正改变字间距的方法
- yum install ncurses-devel libevent-devel openssh-clients
- Bootstrap(七) Bootstrap支持的JavaScript插件
- Postgres数据库是否可以存放数组形式的数据
- 关于TextView分段截取并响应点击(或者将字符串集合拼接在在一个Textview中并分段响应)
- 子网掩码与子网划分两个分别是什么意思
- 有return的情况下try catch finally的执行顺序
- CVE-2015-7547 分析代码bug
- 关于UITextView上部出现空白一步解决