hcg_21 2013-08-07 03:28 采纳率: 0%
浏览 1984

运行时添加按钮的问题

我想在程序运行时在 TableRow 中添加一些按钮, TableRow 有下面的结构:

<TableRow 
            android:layout_marginTop="100px" 
            android:gravity="bottom" 
            android:paddingTop="50px" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:id="@+id/riga1">

            <Button 
                android:textSize="32px" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:id="@+id/buttonOK2" 
                android:text="YES"
                android:onClick="setResult"/>
        </TableRow>

我的 java 代码是:

TableRow tr = (TableRow) findViewById(R.id.riga1);
        Button b = new Button(this);  
        b.setText("button 1");  
        b.setId(1);
        b.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));   
        tr.addView(b);

        Button b1 = new Button(this);  
        b1.setText("button 2");  
        b1.setId(2);
        b1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));   
        tr.addView(b1);

但现在还是不能运行,在 activity 中我只能看见 xml文件中定义的按钮。

另一个问题是如果我在相关的 TableLayout 中设置参数:

android:stretchColumns="*" 

我必须在之前提到的 TableLayout 中的 TableRow 里至少插入一个按钮,否则会在streatchColumns 附近获得一个 "division by zero error"错误。如何能让 tableRow 为空?

  • 写回答

1条回答

  • 码密码密哄 2013-08-07 03:44
    关注

    是不是TableRow 也需要代码写?

    评论

报告相同问题?

悬赏问题

  • ¥15 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。