![](/img/trans.png)
[英]ArithmeticException: Divide by zero with table layout dynamically in Android
[英]Android: How to equally divide the table layout height to table rows in programmatically
我正在動態創建tablerow(9行)。 我想將表格布局高度平均划分為表格行。 但是它不起作用...
我正在創建9行,每行包含9個TextView
我的xml是
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="100" >
<TableLayout
android:layout_weight="80"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:background="#000"
android:paddingTop="1dp"
android:paddingLeft="1dp"
android:paddingRight="1dp"
android:paddingBottom="1dp"
android:id="@+id/sudokuboard"
android:stretchColumns="*"
android:weightSum="100" >
</TableLayout>
我的代碼是
tblsudoku = (TableLayout) findViewById(R.id.sudokuboard);
//1st Row
for(int i=0; i < 9; i++)
{
TableRow NewRow1 =new TableRow(this);
NewRow1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT,10.0f));
NewRow1.setPadding(1, 1, 1, 1);
for(int j=0; j<9; j++)
{
TextView tv_00 = new TextView(this);
int id=10*i;
id=id+j;
String strText=""+id;
tv_00.setText(strText);
tv_00.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT, 10.0f));
tv_00.setGravity(Gravity.CENTER);
tv_00.setBackgroundColor(Color.parseColor("#FFFFFF"));
tv_00.setId(id);
NewRow1.addView(tv_00);
}
//tblsudoku.addView(NewRow1, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
tblsudoku.addView(NewRow1);
}
請幫我解決這個問題...
2013年12月29日更新
新密碼
tblsudoku = (TableLayout) findViewById(R.id.sudokuboard);
for(int i=0; i < 9; i++)
{
TableRow NewRow1 =new TableRow(this);
NewRow1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT,1f));
NewRow1.setBackgroundColor(Color.RED);
LinearLayout Newrowlayout =new LinearLayout(this);
Newrowlayout.setLayoutParams(new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT,1f));
Newrowlayout.setOrientation(1);
NewRow1.setPadding(1, 1, 1, 1);
for(int j=0; j<9; j++)
{
TextView tv_00 = new TextView(this);
int id=10*i;
id=id+j;
String strText=""+id;
tv_00.setText(strText);
tv_00.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT, 1f));
tv_00.setGravity(Gravity.CENTER);
tv_00.setTextColor(Color.YELLOW);
tv_00.setId(id);
Newrowlayout.addView(tv_00);
}
NewRow1.addView(Newrowlayout);
tblsudoku.addView(NewRow1);
}
設計
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000"
android:paddingTop="1dp"
android:paddingLeft="1dp"
android:paddingRight="1dp"
android:paddingBottom="1dp"
android:id="@+id/sudokuboard">
</TableLayout>
關鍵句是“小部件必須具有其父項的LayoutParams”,因此您必須將表行的TableRow.LayoutParams更改為TableLayout.LayoutParams。 希望它能起作用!
更多詳情請點擊這里
下次在發布問題之前,請使用右上角的搜索欄。 看看下面的主題,我相信這是您想要的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.