Android添加动态ListView标题

时间:2014-08-29 05:56:50

标签: android xml android-layout view

我有一个列表视图,我正在尝试为它设置标题,使其看起来像一个表头。这是我的列表视图行的XML文件:

PS:我删除了一些代码来缩短问题

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/txtDisplayDate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" />
<TextView
    android:id="@+id/txtDisplayCat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" />
</LinearLayout>

我的标题列表视图的XML文件:

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/txtDisplayDate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Date" />
<TextView
    android:id="@+id/txtDisplayCat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Category" />

我填充列表视图的类:

private class ListAdapter extends BaseAdapter {
    LayoutInflater inflater;
    ViewHolder viewHolder;

    public ListAdapter(Context context) {
        // TODO Auto-generated constructor stub
        inflater = LayoutInflater.from(context);
        LinearLayout listHeaderView = (LinearLayout)inflater.inflate(
                R.layout.trans_header_layout, null);
        listview.addHeaderView(listHeaderView);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return _translist.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {     
        // TODO Auto-generated method stub
        if (convertView == null) {

            convertView = inflater.inflate(R.layout.trans_listview_row,
                    null);
            viewHolder = new ViewHolder();

            viewHolder.txt_ddate = (TextView) convertView
                    .findViewById(R.id.txtDisplayDate);
            viewHolder.txt_dcat = (TextView) convertView
                    .findViewById(R.id.txtDisplayCat);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.txt_ddate.setText(_translist.get(position).getDate()
                .trim());
        viewHolder.txt_dcat.setText(_translist.get(position)
                .getCategory().trim());
        return convertView;
    }
}

但是,通过使用这些代码,这是我得到的输出:

enter image description here

标题与其下方的列不匹配。我想知道是否有任何替代方法可以将标题添加到列表视图中,使其看起来像一个表头。

提前致谢。

修改

listview所在的我的transaction_rec.XML:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Date" />

        <TextView
            android:id="@+id/txtDisplayDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Category" />

        <TextView
            android:id="@+id/txtDisplayCat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>  
</LinearLayout>

<ListView
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</ListView>
</LinearLayout>

我的transactionRec.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.transaction_rec);
    context = this;
    listview = (ListView) findViewById(R.id.listview);

    //Code to get data from database
}

private class ListAdapter extends BaseAdapter {
    LayoutInflater inflater;
    ViewHolder viewHolder;

    public ListAdapter(Context context) {
        // TODO Auto-generated constructor stub
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return _translist.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {     
        // TODO Auto-generated method stub
        if (convertView == null) {

            convertView = inflater.inflate(R.layout.transaction_rec,
                    null);
            viewHolder = new ViewHolder();

            viewHolder.txt_ddate = (TextView) convertView
                    .findViewById(R.id.txtDisplayDate);

            viewHolder.txt_dcat = (TextView) convertView
                    .findViewById(R.id.txtDisplayCat);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.txt_ddate.setText(_translist.get(position).getDate()
                .trim());
        viewHolder.txt_dcat.setText(_translist.get(position)
                .getCategory().trim());

        return convertView;
        }
    }
}

我现在得到的输出:

enter image description here

1 个答案:

答案 0 :(得分:0)

试试这个......

        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" 

包含所有文字视图。