2

我正在尝试根据字符串的长度创建一个 gridView。

每个 textView 都应该显示一个字符 - 一个下划线 - 并假设字符串中没有空格......

我以前在 c# 中做过类似的事情——我创建了一个包含 15 个标签的表格视图,并根据字母的数量,将每个标签向右移动。它看起来或多或少是这样的:

public void setLetters(String str)
        {
            tableLayoutPanel1.Visible = false;
            int x = revah(str); //revah returns the number of letters in the string
            int yy = x / 2;
            int count = 0;
            if (count <= x)
            {
                tableLayoutPanel1.SetColumn(lbl0, yy);
                count++;
            }
            if (count <= x)
            {
                tableLayoutPanel1.SetColumn(lbl1, yy);
                count++;
            }
            if (count <= x)
            {
                tableLayoutPanel1.SetColumn(lbl2, yy);
                count++;
            }
            if (count <= x)
            {
                tableLayoutPanel1.SetColumn(lbl3, yy);
            count++;
        }
        if (count <= x)
        {
            tableLayoutPanel1.SetColumn(lbl4, yy);
            count++;
        }
        if (count <= x)
        {
            tableLayoutPanel1.SetColumn(lbl5, yy);
            count++;
        }
        if (count <= x)
        {
            tableLayoutPanel1.SetColumn(lbl6, yy);
            count++;
        }
        if (count <= x)
        {
            tableLayoutPanel1.SetColumn(lbl7, yy);
            count++;
        }
        if (count <= x)
        {
            tableLayoutPanel1.SetColumn(lbl8, yy);
            count++;
        }
        if (count <= x)
        {
            tableLayoutPanel1.SetColumn(lbl9, yy);
            count++;
        }
        if (count <= x)
        {
            tableLayoutPanel1.SetColumn(lbl10, yy);
            count++;
        }
        if (count <= x)
        {
            tableLayoutPanel1.SetColumn(lbl11, yy);
            count++;
        }
        if (count <= x)
        {
            tableLayoutPanel1.SetColumn(lbl12, yy);
            count++;
        }
        if (count <= x)
        {
            tableLayoutPanel1.SetColumn(lbl13, yy);
            count++;
        }
        if (count <= x)
        {
            tableLayoutPanel1.SetColumn(lbl14, yy);
            count++;
        }

        tableLayoutPanel1.Visible = true;

    } 

我希望在android中做类似的事情,使用gridView,但不知道如何开始......帮助......

4

2 回答 2

1

您可以使用 GridView(由 Activity 膨胀 - http://developer.android.com/reference/android/app/Activity.html)和其他带有 textview 的布局创建布局。之后,您必须实现一个适配器来用字符串填充 GridView(一个 ArrayAdapter)(在方法 getView 中)。getCount() 方法将确定项目的数量。此方法在 ArrayAdapter 类中,您必须重写它们。在这里检查:http: //developer.android.com/reference/android/widget/ArrayAdapter.html

于 2012-12-02T18:32:08.923 回答
1

按照这个链接 ,这将展示如何制作网格视图,你现在想要什么。你必须创建一个方法来读取你的 textview 值,然后制定一个逻辑将这些单词拆分为单个字符,然后将这些字符添加到 string[] 中。希望这会有所帮助

于 2012-12-02T18:37:54.013 回答