0
我想把這樣的數字按鈕。這是我的代碼:
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout layout2 = new LinearLayout(this);
layout2.setOrientation(LinearLayout.HORIZONTAL);
TextView titleView = new TextView(this);
titleView.setText("Hello World!");
layout.addView(titleView);
android.widget.LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT, 1);
android.widget.TableLayout.LayoutParams params = new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.FILL_PARENT, 1);
Button btnConnect = new Button(this);
btnConnect.setText("Connect");
btnConnect.setLayoutParams(param);
layout2.addView(btnConnect);
Button btnDisconnect = new Button(this);
btnDisconnect.setText("Disconnect");
layout2.addView(btnDisconnect);
btnDisconnect.setLayoutParams(param);
layout.addView(layout2);
TableLayout tblLayout = new TableLayout(this);
tblLayout.setOrientation(TableLayout.HORIZONTAL);
TableRow tblrow = null;
for (int i = 1; i <= 9; i++) {
if (i % 3 == 1) {
tblrow = new TableRow(this);
tblLayout.addView(tblrow);
}
Button b = new Button(this);
b.setText("" + i);
tblrow.addView(b);
}
TableRow tr = new TableRow(this);
Button btnZero = new Button(this);
btnZero.setText("0");
Button btnHash = new Button(this);
btnHash.setText("#");
Button btnStar = new Button(this);
btnStar.setText("*");
tr.addView(btnZero);
tr.addView(btnHash);
tr.addView(btnStar);
tblLayout.addView(tr);
layout.addView(tblLayout);
setContentView(layout);
這符合以下標準:
爲了使我的專欄這樣。我用這個代碼:
android.widget.TableLayout.LayoutParams params = new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.FILL_PARENT, 1);
tblLayout.setLayoutParams(param);
但沒有變化。我需要做什麼?這是Layoutparams不足以做到這一點?
您可以在佈局中放置佈局。例如,你可以用「LinearLayout」水平放置頂部按鈕,其餘部分用「GridLayout」放置。 – 2013-03-05 22:17:53
是的,你是對的我需要使用TableLayout。這是必須的 – snnlaynnkrdsm 2013-03-05 22:18:45
是否有你不在XML文件中這樣做的原因?看起來像矯枉過正,在代碼中動態創建它。 – dymmeh 2013-03-05 22:29:33