为什么我的Button不会在我的TableLayout中添加一行?

时间:2015-04-02 20:15:13

标签: android eclipse android-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));

        }
    });
}
}

2 个答案:

答案 0 :(得分:0)

更改后,请尝试在invalidate()上致电TableLayout

答案 1 :(得分:0)

尝试在此行中指定Tablelayout或tablerow布局参数,而不是使其成为通用(来自线性布局)

 LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
              tr.setLayoutParams(lp);