如何使TextView具有maxLines,如果不合适,则水平展开?

问题描述 投票:0回答:1

我不想使用scrollView。我想要的行为是这样:

短文本:

enter image description here

长文本:

enter image description here

android-layout android-constraintlayout android-textview-autosize
1个回答
0
投票

您是否尝试过XML中的minLines?

类似这样:

<TextView
        android:id="@+id/tv_previous_story"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="3dp"
        android:minLines="2" //(could be any minimum amount of lines)
        android:textColor="@color/text_color"
        android:textSize="@dimen/privacy_text_size" />

如果您始终至少有两个单词(或您在XML中设置的最小行数),这对您应该很好,...

如果不是您要搜索的内容,也许您可​​以以编程方式处理它,并根据单词数设置TextView的宽度和高度...不是确切的方法,例如:

void updateWidthAndHeightOfTextView(TextView tv, String t) {
   tv.setWidth(t.length * [bla bla bla]);
   tv.setHeight(tv.getWidth() / [bla bla bla]); //(if you want to change the height too, in this case according to the width)
}

您以自己的方式编写,知道您的需求...

而且,还有this个答案,我实际上并不熟悉,但也可能会回答您的需求...

此外,下次请提出更具体的问题,这样人们就不必费力地试图了解如何为您提供所需的相关答案

希望对您有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.