设置ListView项的高度高度、ListView

2023-09-03 21:57:46 作者:垫脚望北

这应该是很容易的事,但看起来这是非常棘手的。 在我的code我有这样的ListView:

this should be a very easy thing to do, but it looks like it's very tricky. In my code I have this ListView:

<ListView android:id="@+id/sums_list" android:layout_width="fill_parent"
        android:layout_height="0dp" android:layout_weight="1" android:dividerHeight="0dp"></ListView>

这是填充使用该视图中的ArrayAdapter:

That is populated with an ArrayAdapter that uses this view:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cdpb="http://schemas.android.com/apk/res/ale.android.brainfitness"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="55dp">
...
</LinearLayout>

为什么不是项目55dp高?

Why isn't the item 55dp high?

谢谢大家

推荐答案

你是如何膨胀的看法?

How are you inflating the view?

如果您在设置父参数设置为null如下图所示,然后布局参数被忽略。

If you are setting 'parent' parameter to null like below, then layout parameters are ignored.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  ...
  View v = inflater.inflate(R.layout.filtered_trip_row, null);
  …

  return v;
}

过客父膨胀应该像您期望的。

Passing 'parent' to inflate should work as you expect.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  ...
  View v = inflater.inflate(R.layout.filtered_trip_row, parent);
  …

  return v;
}

这说明它的细节: LayoutInflater 的决策意识

This explains it in detail: Making sense of LayoutInflater

要避免让 UnsupportedOperationException异常,您可以添加作为输入参数的充气。例如:

To avoid getting UnsupportedOperationException, you can add false as an input parameter to the inflator. Example:

inflater.inflate(R.layout.filtered_trip_row, parent, false)
 
精彩推荐
图片推荐