Android的ListView的固定高度的项目 [英] Android ListView fixed height item

查看:235
本文介绍了Android的ListView的固定高度的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个小问题。我想创建一个列表中的所有项目有一个固定高度的机器人列表视图活动。

Got a little problem. I'd like to create an android list view activity with all items in the list having a fixed height.

所以,我的项目布局(thread_item.xml)看起来是这样的:

So, my item layout (thread_item.xml) looks like this:

<?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价值的高度。

I set layout_height of the root element to 300dip and expect all items to have the same height, but they don't. When I run the application it looks like the height having a wrap_content value.

此外,活动本身是这样的:

In addition the activity itself looks like this:

公共类ThreadListActivity扩展ListActivity {

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.*/
            }
        });
    }
}

和适配器我用这样的容貌:

And adapter I'm using looks like this:

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。 我使用的是安卓2.3.3,HTC EVO 3D设备上测试和仿真器(都显示相同的结果)。

I can't see why all list items still wrap their content and don't stay 300dip in height. They might be both smaller or bigger then 300dip. I'm using android 2.3.3, testing on HTC Evo 3D device and an emulator (both show same result).

感谢很多提前。

UPD:

由于米格尔和山姆。解决的办法是设置了maxHeight到TextView的,这让我的列表项增加(这将是 + ID / thread_item_ preVIEW ),并设置RelativeLayout的的了minHeight到300dip作为同时,从萎缩prevent吧。

Thanks to Miguel and Sam. The solution is to set maxHeight to the textView, that makes my list item grow (that would be +id/thread_item_preview) and setting the RelativeLayout's minHeight to 300dip as well, to prevent it from shrinking.

推荐答案

如果你从 WRAP_CONTENT 更改行中的所有子视图的高度,以 match_parent

What if you change all the child view heights in the row from wrap_content to match_parent?

从评论

您是否尝试过了minHeight 了maxHeight 属性?例如:

Have you tried the minHeight and maxHeight attributes? For example:

android:minHeight="300dp"

您应该也看Android的罗曼盖伊讨论效率适配器和 getView ()

You should also watch Android's Romain Guy discuss efficiency in adapters and getView().

这篇关于Android的ListView的固定高度的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆