可根据字符串所占的长度(非字符串长度)自动缩小字体大小, 以适应显示区域的宽度
来源:互联网 发布:淘宝网用网银支付 编辑:程序博客网 时间:2024/06/12 01:07
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView;
/**
* 可根据字符串所占的长度(非字符串长度)自动缩小字体大小, 以适应显示区域的宽度
*
* @author sheng
*
*/
public class CustomTextView extends TextView {
// Attributes
private Paint testPaint;
private float cTextSize;
public CustomTextView (Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* Re size the font so the specified text fits in the text box * assuming
* the text box is the specified width.
* 在此方法中学习到:getTextSize返回值是以像素(px)为单位的,而setTextSize()是以sp为单位的,
* 因此要这样设置setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
*/
private void refitText(String text, int textWidth) {
if (textWidth > 0) {
testPaint = new Paint();
testPaint.set(this.getPaint());
//获得当前TextView的有效宽度
int availableWidth = textWidth - this.getPaddingLeft()
- this.getPaddingRight();
float[] widths = new float[text.length()];
Rect rect = new Rect();
testPaint.getTextBounds(text, 0, text.length(), rect);
//所有字符串所占像素宽度
int textWidths = rect.width();
cTextSize = this.getTextSize();//这个返回的单位为px
while(textWidths > availableWidth){
cTextSize = cTextSize - 1;
testPaint.setTextSize(cTextSize);//这里传入的单位是px
textWidths = testPaint.getTextWidths(text, widths);
}
this.setTextSize(TypedValue.COMPLEX_UNIT_PX, cTextSize);//这里制定传入的单位是px
}
};
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
refitText(getText().toString(), this.getWidth());
}
}
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView;
/**
* 可根据字符串所占的长度(非字符串长度)自动缩小字体大小, 以适应显示区域的宽度
*
* @author sheng
*
*/
public class CustomTextView extends TextView {
// Attributes
private Paint testPaint;
private float cTextSize;
public CustomTextView (Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* Re size the font so the specified text fits in the text box * assuming
* the text box is the specified width.
* 在此方法中学习到:getTextSize返回值是以像素(px)为单位的,而setTextSize()是以sp为单位的,
* 因此要这样设置setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
*/
private void refitText(String text, int textWidth) {
if (textWidth > 0) {
testPaint = new Paint();
testPaint.set(this.getPaint());
//获得当前TextView的有效宽度
int availableWidth = textWidth - this.getPaddingLeft()
- this.getPaddingRight();
float[] widths = new float[text.length()];
Rect rect = new Rect();
testPaint.getTextBounds(text, 0, text.length(), rect);
//所有字符串所占像素宽度
int textWidths = rect.width();
cTextSize = this.getTextSize();//这个返回的单位为px
while(textWidths > availableWidth){
cTextSize = cTextSize - 1;
testPaint.setTextSize(cTextSize);//这里传入的单位是px
textWidths = testPaint.getTextWidths(text, widths);
}
this.setTextSize(TypedValue.COMPLEX_UNIT_PX, cTextSize);//这里制定传入的单位是px
}
};
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
refitText(getText().toString(), this.getWidth());
}
}
0 0
- 可根据字符串所占的长度(非字符串长度)自动缩小字体大小, 以适应显示区域的宽度
- 获得qstring字符串的长度(字节长度和所占的像素宽度)
- iOS UILabel根据字符串长度自动适应宽度和高度
- Android中获取字符串长度、宽度(所占像素宽度)的方法
- 根据字符串的实际长度,获取合适长度的字符串(汉字占2个字符的宽度,其他字符占1个字符的宽度)
- 根据宽度来决定显示的字符串长度(C#)
- Android开发经验之获取画在画布上的字符串长度、宽度(所占像素宽度)
- Android开发经验之获取画在画布上的字符串长度、宽度(所占像素宽度)
- 通过字体大小,判定字符串的显示长度
- Android 计算字符串在屏幕上所占的长度和宽度(像素单位)
- Android中获取字符串长度、宽度(所占像素宽度),手动设定字符串换行
- android 根据TextView宽度 自动缩小字体大小 以适应其宽度
- Android中获取字符串长度、宽度(所占像素宽度)
- Android中获取字符串长度、宽度(所占像素宽度)
- 根据字体大小得到字符串显示时的宽度(C#)
- TextView 根据字符串长度设置textview的宽度
- iOS开发 NSString根据字符串一定高度/宽度返回字符串所占的宽度/高度
- 根据宽根据宽度来决定显示的字符串长度 (C#,VS2005)度来决定显示的字符串长度 (C#,VS2005)
- Apache+Tomcat实现负载均衡
- HttpServletRequest各方法的作用及用法
- 如何用一个充气娃娃走上创业成功巅峰--基本流程
- Maven: 'org.apache.maven.artifact.repository.ArtifactRepository' cannot be instantiated
- IntelliJ IDEA 14.1上JFinal开发环境搭建手册
- 可根据字符串所占的长度(非字符串长度)自动缩小字体大小, 以适应显示区域的宽度
- python 线程,GIL 和 ctypes
- Listview中显示不同的视图布局
- 狗日的养老金
- Linux下安装Apache httpd
- 关于exe或dll文件中的字符串资源
- Ⅰ.2.6.4 如何测试 Table Widgets,以及使用外部数据文件(Qt 4)
- django 快速搭建blog
- Java Map排序+解释