获取包含字体大小的文本高度并设置该高度

Goo*_*ofy 6 android paint android-webview

我有一个Hello现在称为文本的文本我需要在文本大小增加的文本中应用字体后立即对此说12或18应用fontsize.

所以现在我需要使用paint获取文本高度,包括字体大小.

我尝试过以下涂料:

String finalVal ="Hello";

Paint paint = new Paint();
paint.setTextSize(18);
paint.setTypeface(Typeface.SANS_SERIF);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL);

Rect result = new Rect();
// Measure the text rectangle to get the height
paint.getTextBounds(finalVal, 0, finalVal.length(), result);
Run Code Online (Sandbox Code Playgroud)

但它不起作用,请帮忙

编辑

我试图根据textheight动态设置webview的高度我得到单行的文本高度,"Hello"但如果文本中有两行"My name is abc and my dads name is xyz and my moms name is 123" now its not getting the proper text height".

Pra*_*rma 11

试试这种方式:

String finalVal ="Hello";

Paint paint = new Paint();
paint.setTextSize(18);
paint.setTypeface(Typeface.SANS_SERIF);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL);

Rect result = new Rect();
paint.getTextBounds(finalVal, 0, finalVal.length(), result);

Log.d("WIDTH        :", String.valueOf(result.width()));
Log.d("HEIGHT       :", String.valueOf(result.height()));
Run Code Online (Sandbox Code Playgroud)

这是输出:

WIDTH        : 40
HEIGHT       : 14
Run Code Online (Sandbox Code Playgroud)

如果我这样做,

String finalVal ="My name is abc and my dads name is xyz and my moms name is 123";
Run Code Online (Sandbox Code Playgroud)

我的输出是:

WIDTH        : 559
HEIGHT       : 18
Run Code Online (Sandbox Code Playgroud)