TextView的breakStrategy ="balanced"工作方式错误.和layout_width ="wrap_content" [英] TextView works wrong with breakStrategy="balanced" and layout_width="wrap_content"

查看:157
本文介绍了TextView的breakStrategy ="balanced"工作方式错误.和layout_width ="wrap_content"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的布局(我删除了一些属性,因为它们确实没有关系,完整的演示项目是

有人知道如何解决此问题吗?

更新

属性 android:breakStrategy ="balanced" 仅适用于 API 23 及更高版本.在我的示例中,文本占用3行,而 balanced 策略使每行的宽度大致相同.

我希望在这种情况下,视线本身的宽度将与最长的线相同.

我正在寻找任何解决方法.我需要像环聊中那样的解决方案.我不知道它是如何工作的.

更新2

不幸的是,接受的答案不适用于RTL文本.

解决方案

之所以发生这种情况,是因为当TextView计算其宽度时,它会将所有文本测量为1行(在您的情况下),并且在此步骤中,它对android:breakStrategy一无所知.因此,它正在尝试利用所有可用空间在第一行尽可能多地呈现文本.

要获取更多详细信息,请检查方法 int想要的(布局布局) 为什么要包装内容在多行TextView中填充父级?

I have such layout (I've removed some attributes, cause they really doesn't matter, full demo project is here):

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"      (* this is important)
        android:layout_height="wrap_content"
        android:breakStrategy="balanced"         (* this is important)
        android:text="@string/long_text" />
</LinearLayout>

The text long_text is quite long, so it would be separated for a few lines.

Two important lines are android:layout_width="wrap_content" and android:breakStrategy="balanced".

I expect that TextView will calculate width according to the actual text width, but it doesn't.

Does anybody know how to fix this?

UPDATE

Attribute android:breakStrategy="balanced" works only for API 23 and higher. In my example text takes 3 lines, and balanced strategy makes each line approximately same width.

I expect that in this case width of view itself will be the same as the longest line.

I'm looking for any workaround. I need solution like in Hangouts chat. I can't figure out how it works.

UPDATE 2

Unfortunately, accepted answer doesn't work with RTL text.

解决方案

This happens because when TextView calculates its width it measure all text as 1 line (in your case) and on this step it knows nothing about android:breakStrategy. So it is trying to utilize all available space to render as much text on first line as it can.

To get more details, please check method int desired(Layout layout) here

To fix it you can use this class:

public class MyTextView extends TextView {
public MyTextView(Context context) {
    super(context);
}

public MyTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    Layout layout = getLayout();
    if (layout != null) {
        int width = (int) Math.ceil(getMaxLineWidth(layout))
                + getCompoundPaddingLeft() + getCompoundPaddingRight();
        int height = getMeasuredHeight();
        setMeasuredDimension(width, height);
    }
}

private float getMaxLineWidth(Layout layout) {
    float max_width = 0.0f;
    int lines = layout.getLineCount();
    for (int i = 0; i < lines; i++) {
        if (layout.getLineWidth(i) > max_width) {
            max_width = layout.getLineWidth(i);
        }
    }
    return max_width;
}

}

which I took here - Why is wrap content in multiple line TextView filling parent?

这篇关于TextView的breakStrategy ="balanced"工作方式错误.和layout_width ="wrap_content"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆