Android中ListView行的动态高度

6
我看到我的ListView出现了奇怪的行为,但我似乎无法解决它。
我有一个ListView,其中填充了一些数据。每一行包含一张图片和三个文本标签。这是我用于行的XML(我在适配器的getView方法中进行膨胀):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:paddingTop="0dip"
     android:paddingBottom="0dip"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal">

    <ImageView android:id="@+id/Icon"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:padding="2dp"
        android:scaleType="fitCenter"
        android:contentDescription="@string/app_icon"/>

    <TextView android:id="@+id/TxtName"
         android:scrollHorizontally="false"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:textColor="@android:color/black"
         android:background="@color/list_bg1"
         android:layout_weight="2"
         android:padding="2dp"/>

     <TextView android:id="@+id/TxtPackage"
         android:scrollHorizontally="false"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:layout_weight="2"
         android:textColor="@android:color/black"
         android:background="@color/list_bg2"
         android:padding="2dp"/>

     <TextView android:id="@+id/TxtSource"
         android:scrollHorizontally="false"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:layout_weight="2"
         android:background="@color/list_bg3"
         android:textColor="@android:color/black"
         android:padding="2dp"/>
</LinearLayout>

现在,文本字段中输入的任何内容都可以是任意长度(从几个字符到80-100个字符),我希望标签只是换行,并且行的高度会扩展以适应换行标签的高度。

然而,标签确实可以正确换行,但是行的高度不会扩展,因此标签底部被截断。这是我的getView方法:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    SetInfo app = (SetInfo)getItem(position);
    if(app == null)
        return null;

    LinearLayout row = (LinearLayout) (convertView == null
               ? LayoutInflater.from(context).inflate(R.layout.listitem, parent, false)
               : convertView);
    ((TextView)row.findViewById(R.id.TxtName)).setText(app.getName());
    ((TextView)row.findViewById(R.id.TxtPackage)).setText(app.getPackage());
    ((TextView)row.findViewById(R.id.TxtSource)).setText(app.getSource());
    if(app.getIcon() != null)
        ((ImageView)row.findViewById(R.id.Icon)).setImageDrawable(app.getIcon());
    return row;
}

如何使行高自动调整以适应所有内容,并且可以设置不同的高度?


在行的布局中,使用android:maxLines=1更好,它可以防止超过一行的文本。 - Samir Mangroliya
你尝试过将android:layout_weight调整为1或其他值吗? - Andro Selva
@Samir:我想要完全相反的效果:标签必须换行,因为我需要所有的文本都能够显示出来。 - Aleks G
@AndroSelva:layout_weight 在哪个组件上使用?我已经用它来调整行内各个子视图的宽度,但如何使用它来调整行的高度? - Aleks G
2个回答

8

尝试将您的TextViews更改为android:layout_height="wrap_content"。


这几乎实现了我的需求。也就是说,行的高度现在已经扩展以允许标签换行,但我失去了通过为不同标签设置不同背景颜色来正确着色列的能力。现在颜色仅延伸到标签换行的范围内,如果另一个标签换行到更多行,则在我的彩色标签下面会出现白色默认背景。有什么想法如何解决这个问题? - Aleks G
我会接受这个答案,因为这最终是我必须要做的 - 然后进行一系列痛苦的处理,以获得我想要的布局。 - Aleks G

5

我曾经遇到过相同的问题,但是@maebe提供的答案对我没有起作用。为了让它正常工作,我不得不在适配器的getView方法中调用requestLayout()方法来刷新我的TextView。

因此,在您的情况下,您需要为每个TextView调用requestLayout()方法:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    // Inflate your view etc.

    row.findViewById(R.id.TxtName).requestLayout();
    row.findViewById(R.id.TxtPackage).requestLayout();
    row.findViewById(R.id.TxtSource).requestLayout();
    return row;
}

我尝试了各种技巧,但只有这一个在我的情况下起作用。


+1 这个 hack 真是太棒了。让我的一天都变得美好了。非常感谢 @muscardinus。 - niranjan94

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接