-3
您好我想添加一些TableRows到TableLayout,TableLayout是在xml文件中聲明的,TableRows只是在java代碼中進行了定義。 Altough也有一些類似的問題,我不能讓它在我的代碼工作:如何以編程方式將表格行添加到表格佈局?
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
public class MainActivity extends Activity {
TableLayout tableLayout;
Button[][] buttons =new Button[9][9];
TableRow tableRow[]=new TableRow[9];
TableRow.LayoutParams layoutParams=new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tableLayout = (TableLayout) findViewById(R.id.tablay);
for(int line=0; line<9; line++){
tableRow[line]=new TableRow(this);
tableRow[line].setLayoutParams(layoutParams);
tableRow[line].setVisibility(View.VISIBLE);
}
for(int line=0; line<9; line++){
for(int col=0; col<9; col++){
buttons[line][col]=new Button(this);
buttons[line][col].setText(""+line+col);
buttons[line][col].setLayoutParams(layoutParams);
buttons[line][col].setVisibility(View.VISIBLE);
tableRow[line].addView(buttons[line][col]);
}
}
for(int i=0; i<9; i++){
tableLayout.addView(tableRow[i], new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
}
setContentView(R.layout.activity_main);
}
}
的應用始終發射後立即終止,我看不出有什麼問題。 感謝您的幫助!
非常感謝!現在它的工作 – Jonas1902
偉大的,投票和接受答案然後:) – Pehlaj
我接受了答案,也upvoted但upvote不會公開顯示,因爲我沒有足夠的聲譽 – Jonas1902