Android的 - 在运行时主要布局添加布局 [英] Android - Adding layout at runtime to main layout

查看:198
本文介绍了Android的 - 在运行时主要布局添加布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在运行时main.xml中添加tableLayout现有的LinearLayout。
我在main.xml中增加了一个EDITTEXT(R.id.editText1)。这里是我的code。它不工作。我得到一个运行时错误(应用程序意外终止)。

I am trying to add a tableLayout at runtime to the existing LinearLayout in main.xml. I have added a editText(R.id.editText1) in the main.xml. Here's my code. Its not working. I get a runtime error (The application has stopped unexpectedly).

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    text = (EditText)findViewById(R.id.editText1);
    TableLayout tblLayout = new TableLayout(this);
    tblLayout.setLayoutParams(new TableLayout.LayoutParams(8,5));
    tblLayout.setPadding(1,1,1,1);

    for(int r=0; r<ROW_COUNT; ++r)
    {
        TableRow tr = new TableRow(this);
        for(int c=0; c<COL_COUNT; ++c)
        {
            int index = r * COL_COUNT + c;
            buttonList.add(new Button(this));
            buttonList.get(index).setText(buttonNames[index]);
            tr.addView(buttonList.get(index), 60, 30);
        }
        tblLayout.addView(tr);
    }

    LinearLayout mainLayout = (LinearLayout)findViewById(R.layout.main);
    mainLayout.addView(tblLayout);        

    setContentView(mainLayout);
}

任何指针将大大AP preciated。谢谢你。

Any pointers would be greatly appreciated. Thanks.

推荐答案

下面线将经过 super.onCreate(savedInstanceState)第一位;

 setContentView(mainLayout);

那么只有 findViewByid

这篇关于Android的 - 在运行时主要布局添加布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆