如何设置动态创建的tablelayout单元格高度&宽度固定 [英] How to set dynamically created tablelayout cell height & width fixed

查看:190
本文介绍了如何设置动态创建的tablelayout单元格高度&宽度固定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android的初学者。如何设置动态创建的表格布局单元格高度&宽度固定。



点击此处 [ ^ ]截图



细胞高度&宽度根据文字大小而变化。我需要将所有单元格大小固定。



XML代码是

I am beginner in android. How to set dynamically created table layout cell height & width fixed.

Click here[^] for screenshot

The cell height & width was changing based on text size. i need all cell size as fixed.

XML code is

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

            android:id="@+id/main"

            android:layout_width="fill_parent"

            android:layout_height="fill_parent"                                 

            android:orientation="vertical"  

            android:weightSum="100"  >
 </LinearLayout>





Java代码





Java Code

Main = (LinearLayout) findViewById(R.id.main);

    Main_1 = new LinearLayout(this);
    LinearLayout.LayoutParams sudoku_1_lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
    Main_1.setLayoutParams(sudoku_1_lp);
    Main_1.setWeightSum(100);
    Main_1.setOrientation(LinearLayout.VERTICAL);
    Main_1.setId(100);

    //Layout for sudoku_board 
    sudokuboard = new LinearLayout(this);
    LinearLayout.LayoutParams sudokuboard_lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,0,80.0f);
    sudokuboard_lp.setMargins(1, 1, 1, 1);
    sudokuboard.setLayoutParams(sudokuboard_lp);
    sudokuboard.setWeightSum(80);
    sudokuboard.setOrientation(LinearLayout.VERTICAL);
    sudokuboard.setBackgroundColor(Color.parseColor("#FF0000"));
    sudokuboard.setId(102);

    tblsudoku = new TableLayout(this);
    TableLayout.LayoutParams sudoku_tbl_lp = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.FILL_PARENT,80.0f);
    sudoku_tbl_lp.setMargins(2, 2, 2, 2);
    tblsudoku.setLayoutParams(sudoku_tbl_lp);
    tblsudoku.setId(103);

    for(int i=0; i < 9; i++)
    {
            TableRow NewRow1 =new TableRow(this);

            TableLayout.LayoutParams n1 = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,0,10.0f);
            if(i==3 || i==6  )
            {
                n1.setMargins(0,2, 0, 0);
            }
            else
            {
                n1.setMargins(0,1, 0, 0);
            }

            NewRow1.setLayoutParams(n1);

            for(int j=0; j<9; j++)
            {
                TextView tv_00 = new TextView(this);
                int id=10*i;
                id=id+j;
                id=id*id*id;
                String strText=""+id;

                tv_00.setText(strText);
                TableRow.LayoutParams r1 = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT, 10.0f);
                if(j==2 || j==5  )
                {
                    r1.setMargins(0, 0, 2, 0);
                }
                else
                {
                    r1.setMargins(0, 0, 1, 0);
                }

                tv_00.setLayoutParams(r1);
                tv_00.setGravity(Gravity.CENTER);                   
                tv_00.setBackgroundColor(Color.GREEN);
                tv_00.setId(id);
                tv_00.setPadding(2, 2, 2, 2);

                NewRow1.addView(tv_00);
            }
            tblsudoku.addView(NewRow1);
    }
    sudokuboard.addView(tblsudoku); 
    Main_1.addView(sudokuboard);
    Main.addView(Main_1);

推荐答案

您是否尝试为表格布局设置固定值?

Have you tried to set fixed value for the table layout?
Display display = getWindowManager().getDefaultDisplay();
	    Point outSize = new Point();
	    display.getSize(outSize);
	    TableLayout.LayoutParams sudoku_tbl_lp = new TableLayout.LayoutParams(outSize.x,outSize.y,80.0f);


首先你不要需要weightSum属性。只需给每一行和每个单元重量相同,例如1.

然后给每一行的高度为0,每个单元格(TextView)的宽度为0.
First of all you do not need the weightSum attribute. Just give every row and cell the same weight, e.g. 1.
Then give every row a height of 0, and every cell (TextView) a width of 0.


这篇关于如何设置动态创建的tablelayout单元格高度&amp;宽度固定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆