繁体   English   中英

如何从自定义ListView适配器正确格式化TableLayout?

[英]How do I correctly format a TableLayout from a Custom ListView Adapter?

更新:对不起,我两次错误地粘贴到list.xml中,现在我的问题是正确的!...

我创建了一个扩展ArrayAdapter的类,在其中重写了getView,并在那里捕获了row.xml中的控件,并使用它们的值适当地对其进行了setText设置。 一切正常,我在这里没有问题。

然后,我试图做的是以表格格式输出输出,因此我可以跨列等,并根据需要将它们全部对齐。 从这里开始我的问题。

因此,我只用了两个xml文件,一个用于列表,一个用于行。 在列表中,我定义了一个TableLayout,除ListView外,其中没有任何内容。 不是我如何设置我的StretchColumns属性? 然后,在row.xml中,我仅定义一个TableRow,因此我希望获得在TableLayout起始和结束标记之间创建的TableRows标记负载,并且一切都会很好。

从我可以看到结果的意义上说,它确实起作用,但是所有TableRows都不与该表的其余部分或其他行对齐。 它们全都变成了适合自己的内容,似乎根本不属于我的TableLayout。

如果我手工创建xml,我希望将其夸大在我的自定义适配器中,并将其放在TableLayout标记之间(并删除ListView),所有显示都将完美地显示,列对齐等。

也许我做错了什么,或者也许我不应该以这种方式使用TableLayout,而应该以不同的方式来做事。

真的很感激与此有关的任何帮助,如果很久以前就一直困惑不解。

这是row.xml

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/tvQty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:text="QTY"
        android:textSize="12dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tvPartName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:paddingLeft="10dip"
        android:text="Part desererregre"
        android:textSize="12dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tvPartCode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:paddingLeft="10dip"
        android:text="PART CODE"
        android:textSize="12dp"
        android:textStyle="bold" />
</TableRow>

这是我的list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/addCallPart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="Button" />

    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dip"
        android:paddingRight="5dip"
        android:stretchColumns="1" >

        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello" />
    </TableLayout>

    <TextView
        android:id="@id/android:empty"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="There are no records." />

</LinearLayout>

这是我的代码,一切正常,但我将其包括在内以使问题更清楚

public class CallPartAdapter extends ArrayAdapter<CallPart> {
    private final Context context;
    private final List<CallPart> values;

    public CallPartAdapter(Context context, List<CallPart> values) {
        super(context, R.layout.row, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.row, parent, false);

        TextView textView1 = (TextView) rowView.findViewById(R.id.tvQty);
        TextView textView2 = (TextView) rowView.findViewById(R.id.tvPartName);
        TextView textView3 = (TextView) rowView.findViewById(R.id.tvPartCode);

        textView1.setText(Integer.toString(values.get(position).Qty));
        textView2.setText(values.get(position).PartName);
        textView3.setText(values.get(position).PartCode);       

        return rowView;
    }   
}

您不能以这种方式使用TableLayout 好吧,让我重新措辞…… 可以,但是不会获得想要的结果。 实际上,您只有一个TableLayout带有一行: ListView 因此, ListView任何项目都不会获得TableLayout任何布局好处。 它们只是ListView项。

您有两个实际选择:

  1. 动态将您的行添加到TableLayout。 您仍然可以使用适配器,但是它并不会给您带来多少好处,因为您只是将其用作List
  2. 修改行布局以提供TableLayout样式的布局,并坚持使用ListView父级。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM