android listview固定高度,Android ListView固定高度项

作者分享了如何在Android ListActivity中实现列表项高度固定的困扰,并提供了解决方案:设置`thread_item_preview`的maxHeight和RelativeLayout的minHeight。

有一点问题.我想创建一个

Android列表视图活动,列表中的所有项目都具有固定的高度.

所以,我的项目布局(thread_item.xml)如下所示:

android:layout_width="fill_parent"

android:layout_height="300dip"

>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="[dummy]"

android:textSize="20dip"

android:textStyle="bold"

/>

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" />

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">

我将根元素的layout_height设置为300dip,并且希望所有项目具有相同的高度,但是它们没有.当我运行应用程序时,它的高度看起来像一个wrap_content值.

此外,活动本身如下所示:

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 {

Activity context;

List items;

public ThreadItemAdapter(Activity context, int textViewResourceId, List 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,以防止它收缩.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值