如何在列表视图中实现表布局

时间:2014-06-16 11:09:58

标签: android listview tablelayout

我想实现类似Bank的PassBook的布局。所以我想我应该在列表视图中实现表格布局。

以下是一个示例存折示例。

日期特定借款信贷余额

12/06/2014撤回200 10000

11/06/2014存款200 10200

2 个答案:

答案 0 :(得分:2)

创建xml布局,如下所示

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" 
    android:orientation="horizontal"
    android:weightSum="3">

<TextView
    android:id="@+id/date"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center"
    android:text="@string/hello_world" />
 <TextView
     android:id="@+id/action_type"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" 
    android:gravity="center"
    android:layout_weight="1"/>
  <TextView
    android:id="@+id/amount"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"
    android:gravity="center" 
    android:layout_weight="1"/>

 </LinearLayout>

将此布局用作ListView的列表项。

答案 1 :(得分:0)

为ListView使用ArrayAdapter。

在那里,您可以为列表项目的布局充气,无论是TableLayout还是其他。

private class MyListAdapter extends ArrayAdapter<SomeObject> {

    public MyListAdapter(Context context, int resource, List<SomeObject> someObjectList) {

        super(context, resource, someObjectList);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view;

        //view recycling
        if (convertView == null) {
            // inflate the single row with whatever layout you like
            view = getLayoutInflater().inflate(R.layout.table_row, parent, false);
        } else {
            view = convertView;
        }


        SomeObject item = getItem(position);

        //fill the view with data
        TextView date = (TextView) view.findViewById(R.id.date);
        date.setText(item.getDate());
        ...

        return view;
     }
     ...
}

这里有一些关于ListViews和ArrayAdapter的内容: https://github.com/thecodepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView 1

相关问题