lhlhiuy 2015-04-08 14:25 采纳率: 0%
浏览 3489

如何在动态添加tablerow并且在tablerow中循环添加按钮

package com.sxu.cs.tracelife;

import android.os.Bundle;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.TableLayout;
import android.widget.TableRow;

public class MainActivity extends Activity {

TableLayout tableLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tableLayout = (TableLayout) findViewById(R.id.life_table);
for(int i = 0;i<30;i++){
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
for(int j = 0;j<30;j++){
Button bn = new Button(this);
bn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
bn.setText("1");
tr.addView(bn);
}
tableLayout.addView(tr);
}
}
}

请大神帮忙看一下这段代码哪出问题了,总是一个tablelayout 没有加进去tablerow

  • 写回答

1条回答 默认 最新

  • danielinbiti 2015-04-08 14:59
    关注

    看着应该是LayoutParams乱了,你这里默认都是ActionBar.LayoutParams,那不对。
    TableRow的LayoutParams应该是TableRow.LayoutParams

     bn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
     像这行,应该
     bn.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
    
    
     tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
     需要 
     tr.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝