【发布时间】:2012-03-01 14:40:18
【问题描述】:
我发现我的 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:maxLines=1 防止超过一行文本
-
你试过把 android:layout_weight 调整为 1 什么的吗?
-
@Samir:我想要完全相反:标签必须换行,因为我需要所有文本都可见。
-
@AndroSelva:layout_weight 在哪个组件上?我已经习惯于调整行内各个子视图的宽度,但是如何使用它来调整行的高度?
标签: android listview row-height