首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在StaticLayout上设置最大行数

在StaticLayout上设置最大行数
EN

Stack Overflow用户
提问于 2018-08-28 00:44:14
回答 3查看 806关注 0票数 2

StaticLayout.Builder是在API23中引入的,我希望目标更低。如何使用StaticLayout的原始构造函数设置最大行数

引用上的属性似乎都是只读的。

EN

回答 3

Stack Overflow用户

发布于 2019-02-26 23:06:56

在API22之后的StaticLayout类中,仍然有一个隐藏的构造函数,它接受许多参数:

代码语言:javascript
复制
 /**
  * @hide
  * @deprecated Use {@link Builder} instead.
  */
 @Deprecated
 public StaticLayout(CharSequence source, int bufstart, int bufend,
                     TextPaint paint, int outerwidth,
                     Alignment align, TextDirectionHeuristic textDir,
                     float spacingmult, float spacingadd,
                     boolean includepad,
                     TextUtils.TruncateAt ellipsize, int ellipsizedWidth,
                     int maxLines)

如您所见,最后一个参数是最大行数。

票数 1
EN

Stack Overflow用户

发布于 2021-03-25 05:21:27

此构造函数是隐藏的,似乎无法将其与旧的API一起使用。我甚至尝试过使用反射来设置私有字段mMaximumVisibleLineCount,但是没有成功。但是我已经找到了一个向后兼容性的解决方案,可以省略字符串的大小并创建StaticLayout,直到lineCount返回所需的值。

代码语言:javascript
复制
fun createStaticLayout(text: String, textWidth: Int, maxLines: Int, paint: TextPaint): StaticLayout =
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        StaticLayout.Builder
            .obtain(text, 0, text.length, paint, textWidth)
            .setAlignment(Layout.Alignment.ALIGN_NORMAL)
            .setMaxLines(maxLines)
            .setEllipsize(TextUtils.TruncateAt.END)
            .build()
    } else {
        var layout: StaticLayout? = null
        var maxLength: Int = min(text.length, 200) // Could be adjusted

        do {
            layout = StaticLayout(
                text.ellipsize(maxLength), paint, textWidth,
                Layout.Alignment.ALIGN_NORMAL, 1f, 0f,
                false
            )
            maxLength -= 10
        } while (layout!!.lineCount > maxLines)

        layout
    }

///.....

fun String.ellipsize(
    size: Int,
    ending: Char? = '…'
): String = if (this.isEmpty() || size <= 0) {
    ""
} else if (length <= size) {
    this
} else {
    this.substring(0, max(size - 1, 0)).let {
        if (ending == null) {
            it
        } else {
            it + ending
        }
    }
}
票数 1
EN

Stack Overflow用户

发布于 2021-04-18 06:19:18

我发现的唯一方法是使用反射来访问第四个构造函数,它接受在最后一个参数中设置maxLines,它对我有效:

代码语言:javascript
复制
try {
                Constructor<StaticLayout> constructor = StaticLayout.class.getConstructor(
                        CharSequence.class, int.class, int.class, TextPaint.class, int.class,
                        Layout.Alignment.class, TextDirectionHeuristic.class, float.class, float.class,
                        boolean.class, TextUtils.TruncateAt.class, int.class, int.class
                );
                constructor.setAccessible(true);
                StaticLayout sl = constructor.newInstance(text, 0, text.length(), textPaint, width,
                        aligment, TextDirectionHeuristics.FIRSTSTRONG_LTR, spacingMult, spacingAdd,
                        includePad, TextUtils.TruncateAt.END, width, maxLines);

            } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException exception) {
                Log.d("TAG_APP", Log.getStackTraceString(exception));
            }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52043673

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档