字体自适应宽度的TextView
来源:互联网 发布:淘宝卖家开通花呗付款 编辑:程序博客网 时间:2024/04/30 00:25
- 项目遇到 TextView宽度有限时,但是需要显示的文字长度不确定,如:recycleview子布局中显示地址。
- ios 原生有自适应textview,android没找到。
- 下面是从网上找的自适应textview,用起来能满足要求,字体会随着长度改变去适应textview的宽度。
- 用法跟原生textview一样,只是xml把textview 换为FontFitTextView
<your_package.FontFitTextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="字体会随着长度改变去适应textview的宽度"/>
- FontFitTextView .java
package your_package;import android.content.Context;import android.graphics.Paint;import android.util.AttributeSet;import android.util.TypedValue;/** * Created by lwj on 2017/9/29. */public class FontFitTextView extends android.support.v7.widget.AppCompatTextView { // Attributes private Paint mTestPaint; private float defaultTextSize; public FontFitTextView(Context context) { super(context); initialize(); } public FontFitTextView(Context context, AttributeSet attrs) { super(context, attrs); initialize(); } private void initialize() { mTestPaint = new Paint(); mTestPaint.set(this.getPaint()); defaultTextSize = getTextSize(); } /* Re size 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 || text.isEmpty()) return; int targetWidth = textWidth - this.getPaddingLeft() - this.getPaddingRight(); // this is most likely a non-relevant call if( targetWidth<=2 ) return; // text already fits with the xml-defined font size? mTestPaint.set(this.getPaint()); mTestPaint.setTextSize(defaultTextSize); if(mTestPaint.measureText(text) <= targetWidth) { this.setTextSize(TypedValue.COMPLEX_UNIT_PX, defaultTextSize); return; } // adjust text size using binary search for efficiency float hi = defaultTextSize; float lo = 2; final float threshold = 0.5f; // How close we have to be while (hi - lo > threshold) { float size = (hi + lo) / 2; mTestPaint.setTextSize(size); if(mTestPaint.measureText(text) >= targetWidth ) hi = size; // too big else lo = size; // too small } // Use lo so that we undershoot rather than overshoot this.setTextSize(TypedValue.COMPLEX_UNIT_PX, lo); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int parentWidth = MeasureSpec.getSize(widthMeasureSpec); int height = getMeasuredHeight(); refitText(this.getText().toString(), parentWidth); this.setMeasuredDimension(parentWidth, height); } @Override protected void onTextChanged(final CharSequence text, final int start, final int before, final int after) { refitText(text.toString(), this.getWidth()); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { if (w != oldw || h != oldh) { refitText(this.getText().toString(), w); } }}
阅读全文
0 0
- 字体自适应宽度的TextView
- TextView宽度一定,实现TextView自适应任何长度的文本
- 宽度字体自适应
- Lable字体自适应宽度
- UILabel的文本调节宽度,以及字体自适应宽度
- 【Android】如何获取TextView的控件宽度以及字体宽度
- 让textview字体大小自适应宽度
- 根据textView的宽度自动伸缩字体的size
- Android textview自适应宽度自动调整字体大小
- UIlable让字体自适应宽度方法
- 聊天对话气泡-字体自适应气泡宽度
- iOS UILabel根据字体,自适应宽度
- 自适应label的宽度
- UILabel的宽度自适应
- 自适应宽度的ListView
- IOS动态获取文本的高度--自适应文本宽度、高度Textview
- 自适应内容的TextView
- textview字体的高度
- shiro学习一 (开涛的跟我学系列 ) 身份验证
- GY-Kit 物联网开发板
- MySQL删除所有表的外键约束、禁用外键约束
- 关于 Markdown
- 替换字符串:str_replace()、substr_replace()函数
- 字体自适应宽度的TextView
- Java碎碎念-浅海拾忆
- 《Android开发者必备知识体系 》写作计划
- 黑客情景题
- 学生信息管理系统二
- 198. House Robber (DP)
- CS231N-14-Reinforcement Learning
- 什么是小程序?做个小程序要多少钱?哪家公司比较靠谱?【湖南知微网络】
- python正则表达式基础以及案例