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

如何让行具有不同的高度,自动调整以适应所有内容?

4

2 回答 2

8

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

于 2012-03-03T07:00:59.000 回答
5

我遇到了山姆问题,@maebe 的回答对我不起作用。为了让它工作,我必须在适配器的 getView 中调用我的 TextView 上的 requestLayout()。

因此,在您的情况下,您必须为每个 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;
}

我尝试了各种技巧,这是唯一对我有用的技巧。

于 2013-03-26T14:23:10.667 回答