有个小问题。我想创建一个 android ListView Activity ,其中列表中的所有项目都具有固定高度。
因此,我的项目布局 (thread_item.xml) 如下所示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="300dip"
>
<TextView android:id="@+id/thread_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="[dummy]"
android:textSize="20dip"
android:textStyle="bold"
/>
<ImageView android:id="@+id/thread_first_image"
android:layout_below="@id/thread_item_title"
android:scaleType="centerInside"
android:maxWidth="100dip"
android:maxHeight="100dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true" />
<TextView
android:id="@+id/thread_item_preview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/thread_first_image"
android:text="[dummy]"
android:textSize="15dip">
</TextView>
</RelativeLayout>
我将根元素的 layout_height 设置为 300dip,并希望所有项目都具有相同的高度,但事实并非如此。当我运行该应用程序时,它看起来像是具有 wrap_content 值的高度。
此外, Activity 本身看起来像这样:
public class ThreadListActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
Code to get items from the storage.
*/
setListAdapter(new ThreadItemAdapter(this, R.layout.thread_item, itemsArray)));
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
/*Start new Activity. Unrelated stuff.*/
}
});
}
}
我使用的适配器看起来像这样:
public class ThreadItemAdapter extends ArrayAdapter<ThreadItem> {
Activity context;
List<ThreadItem> items;
public ThreadItemAdapter(Activity context, int textViewResourceId, List<ThreadItem> items) {
super(context, textViewResourceId, items);
this.context = context;
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inf = this.context.getLayoutInflater();
View result;
if (convertView == null) {
result = inf.inflate(R.layout.thread_item, null);
} else {
result = convertView;
}
TextView tbTitle = (TextView) result.findViewById(R.id.thread_item_title);
TextView tbPreview = (TextView) result.findViewById(R.id.thread_item_preview);
ImageView ivFirstImage = (ImageView) result.findViewById(R.id.thread_first_image);
ThreadItem item = items.get(position);
tbTitle.setText(item.getThreadTitle());
ivFirstImage.setImageBitmap(item.getFirstImage());
SimpleLeadingMarginSpan span = new SimpleLeadingMarginSpan(item.getFirstImage() != null ? 5 : 0, 115); // TODO: const
SpannableString previewText = new SpannableString(item.getThreadPreview());
previewText.setSpan(span, 0, previewText.length(), 0);
tbPreview.setText(previewText);
return result;
}
}
我不明白为什么所有列表项仍然包裹它们的内容并且不保持 300dip 的高度。它们可能都小于或大于 300dip。 我正在使用 android 2.3.3,在 HTC Evo 3D 设备和模拟器上进行测试(两者都显示相同的结果)。
非常感谢。
更新:
感谢 Miguel 和 Sam。解决方案是将 maxHeight 设置为 textView,这会使我的列表项增长(即 +id/thread_item_preview
)并将 RelativeLayout 的 minHeight 也设置为 300dip,以防止它缩小。
最佳答案
当为 convertView 膨胀时,而不仅仅是
result = inf.inflate(R.layout.thread_item, null);
做
result = inf.inflate(R.layout.thread_item, parent, false);
有问题的方法是inflater.inflate(int viewId, ViewGroup parent, boolean attachToRoot)
——因为你没有尊重提供的parent
(在这个case 是 ListView),无论你提供给 listview item 的什么尺寸,默认情况下都会设置为 layout_width=fill_parent, layout_height=wrap_content,忽略你在 xml 中指定的 300dip 高度。通过提供父 View 并传递 false
,inflater 将遵循 300dip 高度,而不将其附加到根(父 View )。
关于Android ListView固定高度项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10387826/