【问题标题】:Why android:weightSum and android:layout_weight doesn't work with a custom adapter?为什么 android:weightSum 和 android:layout_weight 不适用于自定义适配器?
【发布时间】:2018-02-19 17:03:34
【问题描述】:

我正在使用自定义适配器填充包含多个项目的 ListView。

我的做法是这样的:

AdapterDetalleVentas = new adapterDetalleVentas(actDetalleVenta, listaDetalleDeVentas);
lvPagosActDetalleVenta.setAdapter(AdapterDetalleVentas);

地点:

private static List<DetalleVenta> listaDetalleDeVentas;
private adapterDetalleVentas AdapterDetalleVentas;

private class adapterDetalleVentas extends ArrayAdapter<DetalleVenta> {

        Context context;
        List<DetalleVenta> detallesVentas;

        public adapterDetalleVentas(Context context,  List<DetalleVenta> detallesVentas) {
            super(context, 0, detallesVentas);
            this.context = context;
            this.detallesVentas = detallesVentas;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            DetalleVenta detalleVenta = getItem(position);
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.fila_doble_horizontal, null, false);
            }

            TextView tvNombre = (TextView) convertView.findViewById(R.id.tvNombreFilaDobleHorizontal);
            TextView tvCantidad = (TextView) convertView.findViewById(R.id.tvCantidadFilaDobleHorizontal);
            tvNombre.setText(detalleVenta.getNombre());
            tvCantidad.setText(String.valueOf(detalleVenta.getCantidad()));
            return convertView;
        }
    }

fila_doble_horizo​​ntal 的写法如下:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="1">
    <TextView
        android:id="@+id/tvNombreFilaDobleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Nombre"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="25px"
        android:layout_weight="0.85"/>
    <TextView
        android:id="@+id/tvCantidadFilaDobleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Cantidad"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="25px"
        android:layout_weight="0.15"/>
</LinearLayout>

它应该是这样的:

但是当它运行时,应用程序看起来像这样:

Ca 和 Calzado 代表 TextView tvNombre,数字 4 和 23 代表 TextView tvCantidad。

这类似于我没有使用 weightSum 和 layout_weight :

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/tvNombreFilaDobleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Nombre"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="25px"/>
    <TextView
        android:id="@+id/tvCantidadFilaDobleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Cantidad"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="25px"/>
</LinearLayout>

关于如何解决这个问题的任何建议或想法?

【问题讨论】:

  • getView() 中膨胀xml 文件时如果传递parent 而不是null 会发生什么?
  • one 加权尺寸必须恰好是 0dp。不是match_parent 或任何其他值。

标签: android listview adapter android-layout-weight


【解决方案1】:

感谢回答这个问题的人。

这个我也试过了,但问题仍然存在:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<TextView
    android:id="@+id/tvNombreFilaDobleHorizontal"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Nombre"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="25px"
    android:layout_weight="0.85"/>
<TextView
    android:id="@+id/tvCantidadFilaDobleHorizontal"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Cantidad"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="25px"
    android:layout_weight="0.15"/>
</LinearLayout>

问题出在 ListView 所在的布局中:

原来是这样的:

   <ListView
   android:id="@+id/lvPagosActDetalleVenta"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_marginLeft="8px"
                android:layout_marginRight="8px">
            </ListView>

解决方案是在 lvPagosActDetalleVenta 上将 android:layout_width="wrap_content" 更改为 android:layout_width="match_parent"。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    相关资源
    最近更新 更多