android - 在渲染到布局之前获取 TextView 的高度

标签 android textview

找不到任何好的解决方案计算 textview 高度其中设置了文本 在渲染之前 textview 到布局。请帮忙

最佳答案

2 个解决方案

最初使用解决方案 1,后来找到解决方案 2。两者都有效,这确实是您喜欢的。

重要的是确保所有尺寸都正确,因为在 sp 或 px 中混合字体大小会产生很大差异,具体取决于您在哪个屏幕上进行测试。

https://github.com/hanscappelle/SO-3654321 上提供了一个非常基本的示例项目。

使用 TextView 和 MeasureSpec 的解决方案 1

原始问题的主要问题是下面方法中的 TextView 应该配置为我们的 TextView 应该呈现到布局。我认为这个解决方案对许多面临这个问题的人来说很有值(value)。

public static int getHeight(Context context, CharSequence text, int textSize, int deviceWidth, Typeface typeface,int padding) {
            TextView textView = new TextView(context);
            textView.setPadding(padding,0,padding,padding);
            textView.setTypeface(typeface);
            textView.setText(text, TextView.BufferType.SPANNABLE);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
            int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(deviceWidth, View.MeasureSpec.AT_MOST);
            int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            textView.measure(widthMeasureSpec, heightMeasureSpec);
            return textView.getMeasuredHeight();
        }

以及如何使用它的示例:

// retrieve deviceWidth
int deviceWidth;
WindowManager wm = (WindowManager) textView.getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2){
    Point size = new Point();
    display.getSize(size);
    deviceWidth = size.x;
} else {
    deviceWidth = display.getWidth();
}
// the text to check for
String exampleTextToMeasure = "some example text that will be long enough to make this example split over multiple lines so we can't easily predict the final height";
//  some dimensions from dimes resources to take into account
int textSize = getContext().getResources().getDimensionPixelSize(R.dimen.text_size);
int padding = getContext().getResources().getDimensionPixelSize(R.dimen.text_padding);

// final calculation of textView height
int measuredTextHeight = getHeight(getContext(), exampleTextToMeasure, textSize, deviceWidth, TypeFace.DEFAULT, padding); 

使用 TextPaint 和 StaticLayout 的解决方案 2

此方法依赖于 TextPaint 和 StaticLayout,这也为我迄今为止测试过的所有 API 级别提供了可靠的结果。注意尺寸单位;一切都应该以像素为单位!

来源:Measuring text height to be drawn on Canvas ( Android )

    public static int method1UsingTextPaintAndStaticLayout(
            final CharSequence text,
            final int textSize, // in pixels
            final int deviceWidth, // in pixels
            final int padding // in pixels
    ) {

        TextPaint myTextPaint = new TextPaint();
        myTextPaint.setAntiAlias(true);
        // this is how you would convert sp to pixels based on screen density
        //myTextPaint.setTextSize(16 * context.getResources().getDisplayMetrics().density);
        myTextPaint.setTextSize(textSize);
        Layout.Alignment alignment = Layout.Alignment.ALIGN_NORMAL;
        float spacingMultiplier = 1;
        float spacingAddition = padding; // optionally apply padding here
        boolean includePadding = padding != 0;
        StaticLayout myStaticLayout = new StaticLayout(text, myTextPaint, deviceWidth, alignment, spacingMultiplier, spacingAddition, includePadding);
        return myStaticLayout.getHeight();
    }

关于android - 在渲染到布局之前获取 TextView 的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19908003/

相关文章:

java - Libgdx - 纹理过滤器的TexturePacker组合

android-layout - 以编程方式遵循 XML 的 Android TextView

java - 尝试从 TextView 获取值并将其发送到 firebase 引用,但应用程序崩溃了

android - 以编程方式在另一个布局中添加布局

android - "No XML content. Please add a root view or layout to your document"

java - 在 firebase firestore 中获取对象内部的数据?

android - 登录页面中的水平线

Android org.webrtc.VideoRenderer.I420Frame 数组到 PreviewCallback.onPreviewFrame byte[]

android - RemoteViews setLayoutParams?

android - 如何更改android中textview内的文字大小?