当我单击“TableLayout”时,我正在尝试向我的addSplitButton中添加一个新行。不过,当我点击按钮时,它并没有做我能看到的任何事情。如果有人能看我的代码,并指出我的正确方向,这将是非常感谢。
public class TimerActivity extends ActionBarActivity {
//counter created to increment for row number.
int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);
//Initiate the method for adding a split.
initAddSplit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.timer, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void initAddSplit(){
Button addSplit = (Button) findViewById(R.id.addSplitButton);
addSplit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
counter++;
TableLayout tl = (TableLayout) findViewById(R.id.timerSplits);
TableRow tr = new TableRow(TimerActivity.this);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(lp);
EditText etLeft = new EditText(TimerActivity.this);
etLeft.setLayoutParams(lp);
etLeft.setText("Split Name " + counter);
TextView tvCenter = new TextView(TimerActivity.this);
tvCenter.setLayoutParams(lp);
tvCenter.setText("00:00.00");
TextView tvRight = new TextView(TimerActivity.this);
tvRight.setLayoutParams(lp);
tvRight.setText("00:00:00.00");
tr.addView(etLeft);
tr.addView(tvCenter);
tr.addView(tvRight);
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
});
}
}发布于 2015-04-02 20:28:22
尝试在这一行中指定Tablelayout或tablerow布局参数,而不是使其泛化(来自线性布局)
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(lp);发布于 2015-04-02 20:18:55
更改invalidate()后,尝试在TableLayout上调用它。
https://stackoverflow.com/questions/29421681
复制相似问题