Android ListView固定高度项

Dmi*_*riy 9 android android-listview android-relativelayout

有点问题.我想创建一个Android列表视图活动,列表中的所有项目都具有固定的高度.

所以,我的项目布局(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>
Run Code Online (Sandbox Code Playgroud)

我将根元素的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.*/
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在使用的适配器看起来像这样:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我不明白为什么所有列表项仍然包装其内容并且不保持300dip的高度.它们可能比300dip更小或更大.我正在使用android 2.3.3,测试HTC Evo 3D设备和模拟器(两者都显示相同的结果).

非常感谢提前.

UPD:

感谢Miguel和Sam.解决方案是将maxHeight设置为textView,这会使我的列表项增长(即+id/thread_item_preview),并将RelativeLayout的minHeight设置为300dip,以防止缩小.

jos*_*hus 40

为convertView充气时,而不仅仅是

result = inf.inflate(R.layout.thread_item, null);
Run Code Online (Sandbox Code Playgroud)

做

result = inf.inflate(R.layout.thread_item, parent, false);
Run Code Online (Sandbox Code Playgroud)

有问题的方法是inflater.inflate(int viewId, ViewGroup parent, boolean attachToRoot)- 因为你没有尊重提供的parent(在这种情况下是ListView),你提供给listview项的任何维度将默认设置为layout_width = fill_parent,layout_height = wrap_content,忽略300dip您在xml中指定的高度.通过提供父视图并传递false,inflater将遵循300dip高度,而不将其附加到根(父).


Sam*_*Sam 31

如果你改变所有的行中的子视图的高度,从wrap_content对match_parent?


来自评论

你试过minHeight和maxHeight属性吗?例如:

android:minHeight="300dp"
Run Code Online (Sandbox Code Playgroud)

您还应该观看Android的Romain Guy 讨论适配器和效率的问题getView().


Mig*_*tón 7

尝试改变这个

android:layout_height="300dip"
Run Code Online (Sandbox Code Playgroud)

至

android:minHeight="300dip"
Run Code Online (Sandbox Code Playgroud)

这对我来说使用ExpandableListView,所以我想它适用于这种情况.

  • 但这些陈述是平等的 (4认同)
  • 太棒了:)顺便说一句,我想我写了一个错字.它是"android:minHeight",而不是"android:layout_minHeight". (2认同)