2011-01-27 65 views
14

有没有办法让TextView选择一种字体大小,使其填充任何可用的空间?例如:让TextView缩放其字体大小以填充父项?

<LinearLayout 
    layout_width="fill_parent" 
    layout_height="50dip" > 

    <TextView 
    layout_width="fill_parent" 
    layout_height="fill_parent" 
    textSize="scale?" /> 

</LinearLayout> 

感谢

+0

任何机会,你可以发布你创建的内容的代码检查呢?谢谢!! – 2011-08-28 05:05:27

回答

11

没有,有没有内置的字体缩放在TextView。您可能会在TextView的子项中重载onDraw()方法,但TextView不支持自动缩放文本。

在该另一question.

+0

好的,是的,我只是不得不写一些我自己,无赖。 – user291701 2011-02-09 20:07:21

0

正如在回答尼克坎皮恩引述看看,我用了一个循环和静态布局重复,以正确的尺寸。我的要求是,以适应宽度和使用不超过一条线(没有椭圆):

int i = 0; 
    while(i < 2) { 
     layout = new StaticLayout(text, textPaint, w, Alignment.ALIGN_NORMAL, 1, 0, true); 
     i = layout.getLineCount(); 
     textSize += 2; 
     textPaint.setTextSize(textSize); 
    } 
    textPaint.setTextSize(textSize*0.95f); 
+0

如果文本视图不够高,此方法可能会导致文本的顶部被裁剪掉。 – 2014-10-08 03:27:29

0

只是为了更新,从API 26(的Android 8.0)TextViews具有按比例在水平轴和垂直轴

文本的属性
<?xml version="1.0" encoding="utf-8"?> 
<TextView 
    android:layout_width="match_parent" 
    android:layout_height="200dp" 
    android:autoSizeTextType="uniform" /> 

Autosizing TextViews