java - 如何设置动态创建的表格布局单元格高度和宽度固定

标签 java android android-layout

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

点击here截图

enter image description here

单元格高度和宽度根据文本大小而变化。我需要固定所有单元格大小。

XML 代码是

    <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>

我的代码是

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);

最佳答案

首先使用任何算法获取布局中可能的最大数字。
然后相应地决定布局参数并将其设置为 textview 。

int anywidth = 0; //Decide according to possible max text length 
int anyheight = 0; //Decide according to passible max text length
TableRow.LayoutParams r1 = new TableRow.LayoutParams(anywidth, anyheight, 10.0f);

关于java - 如何设置动态创建的表格布局单元格高度和宽度固定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21036428/

相关文章:

android - TextView 安卓 :ellipsize ="end" issue

android - 如何在android中创建应用程序本地化?

java - Android-Week-View 每周异步加载事件

java - 项目上的抽屉导航点击仍保留在主页 fragment 上

java - 创建与 ESB(Web 逻辑,连接工厂)java 的连接

Java Swing 布局未出现

android - 如何将 Admob 广告放置在屏幕底部中心?(使用 java 代码放置。)

java - java中同时输入多个String数据

iphone - 与大多数手机类型共享的电子书(如文本)的最佳通用格式是什么

java - 在 Android 运行时请求位置权限