Android:ListView 内的 TableLayout 中的替代背景颜色

发布于 2024-11-16 20:58:14 字数 2432 浏览 3 评论 0原文

我正在尝试使用 ListView 内的 TableLayout 以网格样式显示一组数据。我想要的是用不同的颜色显示每一行(两种颜色交替)。在 ListView 中使用普通 LinearLayout 时效果很好,但无论出于何种原因,在使用 TableLayout 时我最终都会让所有行具有相同的背景,我认为这是最后一组。

为此,我使用 BaseAdapter,在 getView 函数中我有类似的内容:

public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.list, parent, false);
                holder = new ViewHolder();
                holder.from = (TextView) convertView.findViewById(R.id.from);
                holder.to = (TextView) convertView.findViewById(R.id.to);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            if(position%2==0) {
                convertView.setBackgroundColor(R.color.cell_background);
            }
            //alternate background
            else {
                convertView.setBackgroundColor(R.color.cell_background_alternate);
            }

            // Bind the data efficiently with the holder.
            holder.from.setText(list.get(position)[0]);
            holder.to.setText(list.get(position)[1]);

            return convertView;
        }

现在在我的活动的 main.xml 中,我只有一个 ListView。在用于膨胀列表的 list.xml 中,我有:

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

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView android:id="@+id/from"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_weight="1"/>

        <TextView android:id="@+id/to"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_weight="1"/>
    </TableRow>
</TableLayout>

另外,另一个问题是列没有对齐:当 TableLayout 应该对齐所有列时,它只占用每行、每列所需的空间。我想出的唯一解决方法是设置列的最小宽度,但这并不漂亮。它在这里不起作用有什么原因吗?

2006 年 21 月编辑: cell notaligned

正如您在图片上看到的,列没有对齐(第 2 行和第 3 行是,但这显然是因为他们里面有相同的数据)。

I'm trying to display a set of data in a Grid style, using a TableLayout inside a ListView. What I want is to display each row with a different color (two colors alternatively). It works well when using a normal LinearLayout in the ListView, but for whatever reason when using a TableLayout I end up having all rows having the same background, which I think is the last one set.

For this, I am using a BaseAdapter, and in the getView function I have something like:

public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.list, parent, false);
                holder = new ViewHolder();
                holder.from = (TextView) convertView.findViewById(R.id.from);
                holder.to = (TextView) convertView.findViewById(R.id.to);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            if(position%2==0) {
                convertView.setBackgroundColor(R.color.cell_background);
            }
            //alternate background
            else {
                convertView.setBackgroundColor(R.color.cell_background_alternate);
            }

            // Bind the data efficiently with the holder.
            holder.from.setText(list.get(position)[0]);
            holder.to.setText(list.get(position)[1]);

            return convertView;
        }

Now in my main.xml for the activity, I just have a ListView. In the list.xml used to inflate the list, I have:

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

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView android:id="@+id/from"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_weight="1"/>

        <TextView android:id="@+id/to"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_weight="1"/>
    </TableRow>
</TableLayout>

Also, another problem is that the columns are not aligned: it just takes as much space as it needs for every row, every columns, when a TableLayout should align all columns. The only workaround that I came up with was to set a minimal width for the columns, but that's not pretty. Any reason why it does not work here?

Edit 21/06:
cell not aligned

As you can see on the picture, the columns are not aligned (row 2 and 3 are but it's obviously because they have the same data inside).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七禾 2024-11-23 20:58:14

尝试替换

convertView = mInflater.inflate(R.layout.list,parent,false);

convertView = mInflater.inflate(R.layout.list, null);

对于表格单元格对齐问题,您能否给出一个示例快照,因为我没有得到您想要完成的任务。

try replacing

convertView = mInflater.inflate(R.layout.list, parent, false);

with

convertView = mInflater.inflate(R.layout.list, null);

and for table cell aligning issue can you give an example snapshot bcos I did not get what u trying to accomplish.

南街九尾狐 2024-11-23 20:58:14

我也遇到了同样的问题,即表头和具有数据的表行对齐,但经过大量努力,我发现摆脱此问题的唯一方法是为所有列提供固定大小

I have also faced same problem of alignment of table header and table row having data but after lot of efforts i found the only way to get rid of this problem is to give fix size to all the columns

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文