调整TextView的大小以使用XML包装内容

时间:2014-12-31 14:52:05

标签: android textview

这个问题已被多次询问,但我没有得到任何答案。

如何让textview调整大小以适应大于单行的文本(可能大约5行)?请注意,我正在寻找仅使用XML的解决方案。

<TextView
            android:id="@+id/item_comments_textview_comment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="@string/comments_comment"
            android:textAppearance="?android:attr/textAppearanceSmall" />

以下是我试过的没有任何影响的属性。

android:singleLine="false"
android:maxLines="5"
android:scrollHorizontally="false"
android:layout_height="0dip"
android:layout_weight="1"
android:inputType="textMultiLine"

有人能让我知道如何在不指定恒定高度的情况下使textview增大吗?

我的总XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:orientation="horizontal"
    android:paddingBottom="24dp"
    android:paddingTop="20dp" >

    <ImageView
        android:id="@+id/item_comments_imageview"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:contentDescription="@string/empty"
        android:scaleType="fitXY"
        android:src="@drawable/ic_launcher" />

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/item_comments_author"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginRight="20dp"
                android:layout_weight="0.50"
                android:ellipsize="end"
                android:maxLines="1"
                android:gravity="top"
                android:text="@string/comments_author"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/item_comments_comment_date"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="0.50"
                android:gravity="right"
                android:text="@string/comments_date"
                android:textAppearance="?android:attr/textAppearanceSmall" />
        </LinearLayout>

        <TextView
            android:id="@+id/item_comments_textview_comment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="@string/comments_comment"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </LinearLayout>

</LinearLayout>

我希望textview带有id&#34; item_comments_textview_comment&#34;增长以适应用户给出的评论,最多可达5行。目前它只显示1行并剪切其余文本。

enter image description here

2 个答案:

答案 0 :(得分:0)

我认为您的评论textview(item_comments_textview_comment)受其父级LinearLayout高度属性的限制。将该高度值从match_parent更改为wrap_content

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginRight="16dp"
    android:orientation="vertical" >
 ...

答案 1 :(得分:0)

您应该尝试使用RelativeLayout来设计行元素。它可以帮助您指定每个元素相对于组件的位置来管理您的设计。

它不仅可以帮助提高应用程序的性能。有一个分析可用,这个特别检查一个类似于你的设计。点击此处:Optimizing Layout Hierarchies