XMLファイルを使用してUIを作成する方法を学びましたが、特にLinearLayout以外でXMLファイルを使用せずにプログラムで行う方法を教えてください。
19620 次
3 に答える
15
次のコードを使用して TableLayout を作成します
TableLayout tbl=new TableLayout(context);
以下を使用してテーブル行を作成します
TableRow tr=new TableRow(context);
ビューを表の行に追加
tr.addView(view);
ここでのビューは、TextView または EditText などです。
テーブル行を TableLayout に追加する
tbl.addView(tr);
このように、テーブル レイアウトにさらにテーブル行を追加できます。
于 2011-08-29T05:17:44.017 に答える
4
以下のコード例はHereに示されています。
public class tablelayout extends Activity implements OnClickListener {
/** Called when the activity is first created. */
//initialize a button and a counter
Button btn;
int counter = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setup the layout
setContentView(R.layout.main);
// add a click-listener on the button
btn = (Button) findViewById(R.id.Button01);
btn.setOnClickListener(this);
}
// run when the button is clicked
public void onClick(View view) {
// get a reference for the TableLayout
TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);
// create a new TableRow
TableRow row = new TableRow(this);
// count the counter up by one
counter++;
// create a new TextView
TextView t = new TextView(this);
// set the text to "text xx"
t.setText("text " + counter);
// create a CheckBox
CheckBox c = new CheckBox(this);
// add the TextView and the CheckBox to the new TableRow
row.addView(t);
row.addView(c);
// add the TableRow to the TableLayout
table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
}
于 2011-08-29T05:12:52.103 に答える