StaticLayout.Builder是在API23中引入的,我希望目标更低。如何使用StaticLayout的原始构造函数设置最大行数
引用上的属性似乎都是只读的。
发布于 2019-02-26 23:06:56
在API22之后的StaticLayout类中,仍然有一个隐藏的构造函数,它接受许多参数:
/**
* @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)如您所见,最后一个参数是最大行数。
发布于 2021-03-25 05:21:27
此构造函数是隐藏的,似乎无法将其与旧的API一起使用。我甚至尝试过使用反射来设置私有字段mMaximumVisibleLineCount,但是没有成功。但是我已经找到了一个向后兼容性的解决方案,可以省略字符串的大小并创建StaticLayout,直到lineCount返回所需的值。
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
}
}
}发布于 2021-04-18 06:19:18
我发现的唯一方法是使用反射来访问第四个构造函数,它接受在最后一个参数中设置maxLines,它对我有效:
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));
}https://stackoverflow.com/questions/52043673
复制相似问题