首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么我的按钮不添加一行到我的TableLayout?

为什么我的按钮不添加一行到我的TableLayout?
EN

Stack Overflow用户
提问于 2015-04-02 20:15:13
回答 2查看 35关注 0票数 0

当我单击“TableLayout”时,我正在尝试向我的addSplitButton中添加一个新行。不过,当我点击按钮时,它并没有做我能看到的任何事情。如果有人能看我的代码,并指出我的正确方向,这将是非常感谢。

代码语言:javascript
运行
复制
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));

        }
    });
}
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-02 20:28:22

尝试在这一行中指定Tablelayout或tablerow布局参数,而不是使其泛化(来自线性布局)

代码语言:javascript
运行
复制
 LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
              tr.setLayoutParams(lp);
票数 0
EN

Stack Overflow用户

发布于 2015-04-02 20:18:55

更改invalidate()后,尝试在TableLayout上调用它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29421681

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档