如何调整文本字体大小以适合的TextView [英] How to adjust text font size to fit textview
问题描述
有没有办法在Android的调整TEXTSIZE在一个TextView,以适应它占据的空间?
例如。我使用的是TableLayout和增加几个textviews的每一行。因为我不希望textviews包裹文字我宁愿看到它降低了内容的字体大小。
任何想法?
我已经试过measureText,但因为我不知道该列的大小似乎麻烦的使用。 这是我想改变字体大小的东西,符合
在code 的TableRow行=新的TableRow(本);
的for(int i = 0; I< ColumnNames.length;我++){
TextView的textColumn =新的TextView(本);
textColumn.setText(ColumnNames [I]);
textColumn.setPadding(0,0,1,0);
textColumn.setTextColor(getResources()的getColor(R.drawable.text_default));
row.addView(textColumn,新TableRow.LayoutParams());
}
table.addView(行,新TableLayout.LayoutParams());
该解决方案下面包含了所有的建议,在这里。它从什么最初发布的Dunni。它采用像gjpc的二进制搜索,但它是一个有点更具可读性。它还包括的gregm的bug修复和我自己的错误修复。
进口android.content.Context;
进口android.graphics.Paint;
进口android.util.AttributeSet;
进口android.util.TypedValue;
进口android.widget.TextView;
公共类FontFitTextView扩展TextView的{
公共FontFitTextView(上下文的背景下){
超(上下文);
初始化();
}
公共FontFitTextView(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
初始化();
}
私人无效INITIALISE(){
mTestPaint =新的油漆();
mTestPaint.set(this.getPaint());
//最大大小默认为最初指定的文本大小,除非实在是太小
}
/ *重新调整字体大小,以便指定文本在文本框中适合
*假设文本框中指定的宽度。
* /
私人无效refitText(字符串文本,诠释输出textWidth)
{
如果(输出textWidth&所述; = 0)
返回;
INT targetWidth =输出textWidth - this.getPaddingLeft() - this.getPaddingRight();
浮HI = 100;
浮LO = 2;
最终浮动阈值= 0.5F; //如何接近,我们必须要
mTestPaint.set(this.getPaint());
而((高 - LO)>阈值){
浮动大小=(HI + LO)/ 2;
mTestPaint.setTextSize(大小);
如果(mTestPaint.measureText(文本)> = targetWidth)
喜=大小; // 太大
其他
LO =大小; // 太小
}
//使用LO让我们冲,而不是超调
this.setTextSize(TypedValue.COMPLEX_UNIT_PX,LO);
}
@覆盖
保护无效onMeasure(INT widthMeasureSpec,INT heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec,heightMeasureSpec);
INT上级宽度= MeasureSpec.getSize(widthMeasureSpec);
INT高= getMeasuredHeight();
refitText(this.getText()的toString(),上级宽度。);
this.setMeasuredDimension(上级宽度,高度);
}
@覆盖
保护无效onTextChanged(最终CharSequence的文字,最终诠释开始,最终诠释前,后的最终诠释){
refitText(text.toString(),this.getWidth());
}
@覆盖
保护无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh){
如果(W!= oldw){
refitText(this.getText()的toString(),W);
}
}
//属性
私人油漆mTestPaint;
}
Is there any way in android to adjust the textsize in a textview to fit the space it occupies?
E.g. I'm using a TableLayout and adding several textviews to each row. Since I don't want the textviews to wrap the text I rather see that it lowers the font size of the content.
Any ideas?
I have tried measureText, but since I don't know the size of the column it seems troublesome to use. This is the code where I want to change the font size to something that fits
TableRow row = new TableRow(this);
for(int i=0;i<ColumnNames.length; i++){
TextView textColumn = new TextView(this);
textColumn.setText(ColumnNames[i]);
textColumn.setPadding(0, 0, 1, 0);
textColumn.setTextColor(getResources().getColor(R.drawable.text_default));
row.addView(textColumn, new TableRow.LayoutParams());
}
table.addView(row, new TableLayout.LayoutParams());
The solution below incorporates all of the suggestions here. It starts with what was originally posted by Dunni. It uses a binary search like gjpc's, but it is a bit more readable. It also include's gregm's bug fixes and a bug-fix of my own.
import android.content.Context;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView;
public class FontFitTextView extends TextView {
public FontFitTextView(Context context) {
super(context);
initialise();
}
public FontFitTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initialise();
}
private void initialise() {
mTestPaint = new Paint();
mTestPaint.set(this.getPaint());
//max size defaults to the initially specified text size unless it is too small
}
/* 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)
return;
int targetWidth = textWidth - this.getPaddingLeft() - this.getPaddingRight();
float hi = 100;
float lo = 2;
final float threshold = 0.5f; // How close we have to be
mTestPaint.set(this.getPaint());
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) {
refitText(this.getText().toString(), w);
}
}
//Attributes
private Paint mTestPaint;
}
这篇关于如何调整文本字体大小以适合的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!