Android开发库VUtils之文字大小自适应TextView
来源:互联网 发布:东京大学医学部 知乎 编辑:程序博客网 时间:2024/06/03 16:37
很多时候我们需要文字大小随控件的宽变小,以保证文字能完整显示,现自定义控件如下:
package com.v.vutils.views;import android.content.Context;import android.graphics.Paint;import android.text.TextPaint;import android.util.AttributeSet;import android.view.Gravity;import android.widget.TextView;public class FitTextView extends TextView { private Paint mTextPaint; private float mMaxTextSize; // 获取当前所设置文字大小作为最大文字大小 private float mMinTextSize = 8; public FitTextView(Context context) { this(context, null); } public FitTextView(Context context, AttributeSet attrs) { super(context, attrs); setGravity(getGravity() | Gravity.CENTER_VERTICAL); // 默认水平居中 setLines(1); initialise(); } @Override protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { refitText(text.toString(), this.getWidth()); super.onTextChanged(text, start, lengthBefore, lengthAfter); } private void initialise() { mTextPaint = new TextPaint(); mTextPaint.set(this.getPaint()); // max size defaults to the intially specified text size unless it is too small mMaxTextSize = this.getTextSize();// mMinTextSize = 8; } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { if (w != oldw) { refitText(this.getText().toString(), w); } } /** * Resize the font so the specified text fits in the text box * assuming the text box is the specified width. * */ private void refitText(String text, int textWidth) { if (textWidth > 0) { int availableWidth = textWidth - this.getPaddingLeft() - this.getPaddingRight(); float trySize = mMaxTextSize; mTextPaint.setTextSize(trySize); while (mTextPaint.measureText(text) > availableWidth) { trySize -= 1; if (trySize <= mMinTextSize) { trySize = mMinTextSize; break; } mTextPaint.setTextSize(trySize); } // setTextSize参数值为sp值 setTextSize(px2sp(getContext(), trySize)); } } /** * 将px值转换为sp值,保证文字大小不变 */ public static float px2sp(Context context, float pxValue) { float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (pxValue / fontScale); }}
xml中使用,同TextView使用完全一样:
<com.v.vutils.views.FitTextView android:layout_width="200dp" android:layout_height="wrap_content" android:text="80000000" android:textSize="36sp" />
0 0
- Android开发库VUtils之文字大小自适应TextView
- Android开发库VUtils之农历计算
- Android开发库VUtils之ViewHolder
- Android开发库VUtils之MD5加密
- Android开发库VUtils之圆形头像CircleImageView
- Android开发库VUtils之关联代码行的log
- Android开发库VUtils之dp、sp转px
- TextView文字大小自适应
- UILabel文字大小自适应之--SizeToFit
- UILabel文字大小自适应之--SizeToFit
- UILabel文字大小自适应之--SizeToFit
- android开发之TextView
- Android O新特性之Autosizing TextViews(随着TextView宽高而自动改变文字大小)
- Android自适应大小TextView
- Android控件开发之TextView
- Android控件开发之TextView
- android 屏幕适配之自适应字体大小的TextView
- Android TextView 实现文字大小不同和文字颜色不同
- Xcode快捷键
- TodayWidget使用及创建
- 搭建可用http访问的svn(windows)
- jquery实现分页
- UI全屏设置
- Android开发库VUtils之文字大小自适应TextView
- wireshark抓包中Checksum Offload问题
- iOS 关于navigationBar的一些:毛玻璃、透明、动态缩放、动态隐藏
- 查找(Searching)
- 后台界面也可以很酷!31个高大上的后台管理系统模版
- 利用cache特性检测Android模拟器
- 美团ios navigationBar渐变效果
- 1000行代码写小游戏(七)
- 译:WebRTC视频通信