我发现我的 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;
}
如何让行具有不同的高度,并自动调整以适合所有内容?
最佳答案
尝试将您的 TextView 更改为具有 android:layout_height="wrap_content"。
关于android - android中ListView行的动态高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9518144/