【发布时间】: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_horizontal 的写法如下:
<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