有一点问题。我想创建一个android列表视图活动,列表中的所有项目都有固定的高度。Android ListView固定高度项目
所以,我的项目布局(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值的高度。
除了活动本身看起来是这样的:
而且我用这样的长相适配器:
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设备和模拟器上进行测试(两者都显示相同的结果)。
非常感谢。
UPD:
由于米格尔和山姆。解决方案是将maxHeight设置为textView,这使得我的列表项目增长(这将是+id/thread_item_preview
)并将RelativeLayout的minHeight也设置为300dip,以防止它缩小。
如果我们将最小高度和最大高度设置为相同,那么当列表视图中文本的大小超过该高度时会发生什么? – Ashwin