如何制作10X6网格,每个单元格在android中填充文本视图?

时间:2012-03-28 05:35:16

标签: android android-layout

我是android的新手。请告诉我如何制作一个包含10行和6列的网格,每个单元格都填充了文本视图。我想用不同的值填充每个单元格的文本,点击按钮。 我使用表格布局制作网格,有10行和60个文本视图,但我认为这不是正确的方法。 请提供此源代码。 在此先感谢...我的java文件..和xml文件在这里...

    TableLayout t1 =(TableLayout)findViewById(R.id.myTableLayout)
// TableRow [] tr = new TableRow[10];  
    TableRow tr = new TableRow(this);
    TextView [] tv = new TextView[20];
    int j=0;
    int k=0;
   //for inserting 10 rows..
    for(int i=0;i<=10;i++)
    {
        String cnt; 
        tv[j] = new TextView(this);
       TextView tv1 = new TextView(this);
       TextView ttv = new TextView(this);
     //  TextView tv = new TextView(this);
        //to put a space between cells
    //tv1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        tv1.setPadding(20, 0, 0, 0); 
        tv1.setWidth(30);
        tv1.setHeight(45);
        tv1.setText("");


         //to put digits in text views...      
    //ttv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        ttv.setPadding(20, 0, 0, 0); 
        ttv.setWidth(60);
        ttv.setHeight(45);
        ttv.setTextColor(Color.DKGRAY);
        ttv.setBackgroundColor(Color.CYAN);
        ttv.setTextSize(25);
        cnt=""+k+"";
        ttv.setText(cnt);
        k++;

        // to put symbols store in an array using a button click...the code of button and array in dnt show here...
   // tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        tv[j].setPadding(20, 0, 0, 0); 
        tv[j].setWidth(60);
        tv[j].setHeight(45);
        tv[j].setTextColor(Color.YELLOW);
        tv[j].setBackgroundColor(Color.GREEN);
        tv[j].setTextSize(25);
        //tv[j].setText("ab");

        tr.setPadding(0, 1, 0, 1); 

        tr.addView(tv1);
        tr.addView(ttv);
        tr.addView(tv[j]);

    j++;
    tr.addView(tv1);
        tr.addView(ttv);
        tr.addView(tv[j]);

    j++;
    tr.addView(tv1);
        tr.addView(ttv);
        tr.addView(tv[j]);   
        t1.addView(tr);

    }   

2 个答案:

答案 0 :(得分:1)

你不会得到codez。所以不要问它。我很早就学会了,所以建议你自己尝试编码。只有当我们看到你确实自己做了一些事情时,我们才会帮助你。

无论如何,使用GridLayout会更好。在您的布局和按钮的TextView中添加onClick(),只需更改相应文本视图的文字 -

tv1.setText("1st changed");
tv2.setText("2nd changed");

等等。

当然,您需要找到tv1tv2 ...也。

TextView tv1=(TextView) findViewById(R.id.tv1);

答案 1 :(得分:0)

在您的XML for gridview中再设置一个属性

android:numColumns="6"

用于连续设置6列。

然后set adapter with length of 60 element,您将得到10X6网格