我在我的TableLayout
中放置了60個按鈕,這些按鈕超出了屏幕,因此我需要滾動才能顯示剩餘的按鈕。我試過這段代碼,但應用程序不工作。以編程方式滾動TableLayout
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TableLayout tableLayout;
tableLayout = (TableLayout) findViewById(R.id.table_layout);
TableRow tr;
int buttonNumber = 0;
for (int i =0;i<30;i++)
{
tr = new TableRow(this);
for (int j =0;j<2;j++)
{
buttonNumber++;
final Button btn = new Button(this);
btn.setText("Button" + (buttonNumber));
btn.setTag(buttonNumber);
btn.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.MATCH_PARENT,1));
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
tr.addView(btn);
}
tableLayout.addView(tr);
}
ScrollView sv = new ScrollView(this);
sv.addView(tableLayout);
super.setContentView(sv);
}
如何以編程方式創建可滾動的TableLayout
?
你能不能添加R.layout.activity_main的xml代碼?所以我可以看到是否有錯誤 –