Установка высоты элемента ListView - программирование
Подтвердить что ты не робот

Установка высоты элемента ListView

Это должно быть очень легко сделать, но похоже, что это очень сложно. В моем коде у меня есть 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, который использует это представление:

<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 не высок?

Спасибо всем

4b9b3361

Ответ 1

Как вы раздуваете представление?

Если вы устанавливаете параметр "parent" равным null, как показано ниже, параметры макета игнорируются.

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

  return v;
}

Передача "parent" для раздувания должна работать так, как вы ожидаете.

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

  return v;
}

Это объясняет это подробнее: Почему LayoutInflater игнорирует параметры макета layout_width и layout_height, которые я указал?

Чтобы избежать получения UnsupportedOperationException, вы можете добавить false в качестве входного параметра в inflator. Пример:

inflater.inflate(R.layout.filtered_trip_row, parent, false)

Ответ 2

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

  return v;
}

просто передать значение boolean в метод, чтобы он добавил yourrowlayout в listview и он не даст ошибки, как ошибка java.lang.UnsupportedOperationException: addView (View, LayoutParams) не поддерживается в AdapterView

и добавьте yourLayout, которое вы хотите заполнить с помощью адаптера

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

он работает для меня, и я думаю, что это может помочь вам

Ответ 3

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:minHeight="60dp"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center"
    android:weightSum="1"
    android:background="@color/main_color">

    <ImageView
        android:id="@+id/img_left_menu"
        android:layout_weight="0.4"
        android:focusable="false"
        android:maxHeight="50dp"
        android:maxWidth="50dp"
        android:minHeight="50dp"
        android:minWidth="50dp"
        android:layout_width="0dp"
        android:scaleType="centerInside"
        android:adjustViewBounds="true"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/tx_left_menu"
        android:layout_width="0dp"
        android:textSize="18dp"
        android:layout_gravity="center_vertical"
        android:layout_weight="0.6"
        android:singleLine="true"
        android:layout_height="wrap_content"
        android:focusable="false"
        />
</LinearLayout>

в адаптере установите родительский элемент, как описано выше

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

  return v;
}

Ответ 4

convertView = inflater.inflate(R.layout.custom_row, parent, false);

if (CONDITION) {
    holder.wholeLayout.getLayoutParams().height = 1; // visibility Gone not working && 0 height crash app.
} else {
    holder.wholeLayout.getLayoutParams().height = RelativeLayout.LayoutParams.WRAP_CONTENT;
}