0

我正在使用一个简单的ListView,其中每个项目只包含文本字段而没有其他内容。 下面是列表项我的XML代码:Android如何使列表项高度增加以容纳内容

<?xml version="1.0" encoding="utf-8"?> 
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="?android:attr/listPreferredItemHeight"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="6dp" 
      android:layout_marginRight="6dp" 
      android:layout_marginTop="4dp" 
      android:layout_marginBottom="4dp" 
      android:background="@drawable/bg_card"> 


     <TextView 
      android:id="@+id/txt" 
      android:layout_width="fill_parent" 
      android:layout_height="50dp" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:gravity="center_vertical" 
      android:textSize="15sp" 
      android:paddingLeft="6dip" 
      android:textColor="#878787" 
      android:textIsSelectable="true"/> 

     </LinearLayout> 

    </FrameLayout> 

这里是我使用列表视图中的片段布局。

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#e5e5e5"> 

    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginLeft="16dp" 
     android:layout_marginRight="16dp" 
     android:dividerHeight="0dp" 
     android:divider="@null"/> 

    </RelativeLayout> 

现在,虽然在我的片段像这样

Level weather_data[] = new Level[] { 
    new Level(R.drawable.s1, 
       "Some text which will be present in the list item",  
       R.drawable.play_button) 
     }; 

创建适配器的对象。如果我有即文本的第二个参数很长的句子,它的某些部分没有得到显示并且该句子在列表项中不完整地显示。

我已经尝试使用android:minHeight来更改列表项的高度,但它不起作用。有什么可以解决这个问题的? 谢谢

+0

我想这是因为你给TextView的高度50dp.Make它WrapContent采取文字的大小。 –

+0

@ user2012不,它仍然没有任何区别。 –

+0

试试这个http://stackoverflow.com/questions/5033012/auto-scale-textview-text-to-fit-within-bounds – VikramV

回答

0

将容器设置为wrap_content就足够了,它将在需要时调整大小。您的显示布局是不完整的,我只是怀疑你可能有一些在这里hardfixed:

android:layout_height="?android:attr/listPreferredItemHeight 

还没有真正的点,你布局LinearLayoutTextView,然后都在FrameLayout。你可以与填充和BG同样的效果搬到FrameLayout

+0

对不起,但我不明白什么,我应该设置为“wrap_content”? –

0

尝试设置wrap_content而不是"?android:attr/listPreferredItemHeight"android:layout_height

事情是这样的:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
+0

不,它不工作。我仍然面临同样的问题。 –

+0

任何其他方式来纠正这个问题? –

相关问题