【问题标题】:resizing height of textview to match content调整 textview 的高度以匹配内容
【发布时间】:2012-12-16 04:02:32
【问题描述】:

我正在向下面的 textView 动态添加内容并且它可以工作。唯一的问题是,我只看到一行文字。如果我通过 layout.xml 或使用字符串将 textView 的文本设置为 25 行长,然后动态地用新文本替换它,那么新文本将只有 25 行长,即使它实际上是 50 行。我将 minLines 设置为一个很高的数字,但这对我来说是草率的代码。知道我在这里缺少什么吗?

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/progressBar1"
    android:layout_weight="1" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="match_parent" />
</ScrollView>

【问题讨论】:

  • fill_parentmatch_parent 是同义词。 match_parent 替换了 fill_parent,因为它更准确地反映了视图的大小。

标签: android android-layout textview scrollview settext


【解决方案1】:

代替:

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="match_parent" />

尝试使用:

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

这将使 TextView 扩展到其内容所需的高度。同时,ScrollView 会根据滚动位置处理显示的内容。

【讨论】: