简体   繁体   English

自动调整Android的Textview

[英]Auto Adjust Textview Android

I finally found the code to resize the text based on the height of the textview. 我终于找到了根据textview的高度调整文本大小的代码。 Source , but i am not getting how to pass the text in this function,my text is cmoing from another intent 来源 ,但是我没有在此函数中传递文本,我的文本来自另一个意图

public int getHeightOfMultiLineText(String text,int textSize, int maxWidth) {
        TextPaint paint = new TextPaint();
        paint.setTextSize(textSize);
        int index = 0;
        int linecount = 0;
        while(index < text.length()) {
            index += paint.breakText(text,index,text.length(),true,maxWidth,null);
            linecount++;
        }

        Rect bounds = new Rect();
        paint.getTextBounds("Yy", 0, 2, bounds);
        // obtain space between lines
        double lineSpacing = Math.max(0,((linecount - 1) * bounds.height()*0.25)); 

        return (int)Math.floor(lineSpacing + linecount * bounds.height());

    }

Well you can get the text of TextView as String using urTextView.getText().toString() 好了,您可以使用urTextView.getText().toString()将TextView的文本作为字符串urTextView.getText().toString()

TextView urTextView;
String text = urTextView.getText().toString();

int textSize = 100;
int maxHeight = 50;
while(getHeightOfMultiLineText(text,textSize,maxWidth) > maxHeight)
    textSize--;

This should work. 这应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM