为什么我的 Button 不会向我的 TableLayout 添加行?
Why will my Button not add a row to my TableLayout?
我试图在单击 "addSplitButton" 时向我的 TableLayout 添加新行。但是,当我单击该按钮时,它不会执行任何我能看到的操作。如果有人可以查看我的代码并为我指出正确的方向,将不胜感激。
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));
}
});
}
}
更改后尝试在 TableLayout
上调用 invalidate()
。
尝试在此行中指定 Tablelayout 或 tablerow 布局参数而不是使其通用(来自线性布局)
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(lp);
我试图在单击 "addSplitButton" 时向我的 TableLayout 添加新行。但是,当我单击该按钮时,它不会执行任何我能看到的操作。如果有人可以查看我的代码并为我指出正确的方向,将不胜感激。
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));
}
});
}
}
更改后尝试在 TableLayout
上调用 invalidate()
。
尝试在此行中指定 Tablelayout 或 tablerow 布局参数而不是使其通用(来自线性布局)
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(lp);