我是android的初学者。如何设置动态创建的表格布局单元格高度&宽度固定。
点击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);
答案 0 :(得分:0)
首先使用任何算法在你的布局中获得最大可能的数字 然后相应地决定布局参数并将其设置为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);