2014-12-31 52 views
0

這個問題已經被問到了多少次,但是我沒有從我那裏得到任何答案。調整TextView的大小以使用XML來包裝內容

如何獲取textview以調整自身大小以適應大於單行的文本(可能爲5行左右)?請注意,我正在尋找解決方案來處理XML ONLY。

<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> 

我想ID爲「item_comments_textview_comment」增長,以適應用戶給出的評論,以高達最多5行的TextView。目前它只顯示1行並剪切剩下的文本。

enter image description here

回答

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" > 
... 
+0

它的工作原理..很多 –

0

你應該嘗試使用的RelativeLayout爲行元素的設計。它可以幫助您指定每個元素相對於組件的位置來管理您的設計。

不僅它可以幫助提高應用程序的性能。這裏有一個分析,這一個特別檢查一個類似於你的設計。在這裏檢查:Optimizing Layout Hierarchies