在动态创建的表下动态创建按钮

时间:2015-04-22 18:35:18

标签: android

我动态创建一个表:

标题

void addHeader() {
        /** Create a TableRow dynamically **/
        tr = new TableRow(this);
        /** Creating a TextView to add to the row **/
        label = new TextView(this);
        label.setText("Inventarnummer");
        label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        label.setPadding(5, 5, 5, 5);
        LinearLayout Ll = new LinearLayout(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        params.setMargins(5, 5, 5, 5);
        // Ll.setPadding(10, 5, 5, 5);
        Ll.addView(label, params);
        tr.addView((View) Ll); // Adding textView to tablerow.

        /** Creating Qty Button **/
        TextView place = new TextView(this);
        place.setText("Bezeichnung");
        place.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        place.setPadding(5, 5, 5, 5);
        // place.setBackgroundColor(Color.RED);
        Ll = new LinearLayout(this);
        params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 5, 5);
        // Ll.setPadding(10, 5, 5, 5);
        Ll.addView(place, params);
        tr.addView((View) Ll); // Adding textview to tablerow.

        /** Creating Qty Button **/
        TextView place1 = new TextView(this);
        place1.setText("Betriebszahl");
        place1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        place1.setPadding(5, 5, 5, 5);
        // place.setBackgroundColor(Color.RED);
        Ll = new LinearLayout(this);
        params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 5, 5);
        // Ll.setPadding(10, 5, 5, 5);
        Ll.addView(place1, params);
        tr.addView((View) Ll); // Adding textview to tablerow.

        // Add the TableRow to the TableLayout
        tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
    }

按钮在桌面下是静态的,在ScrollView内部,但如果表格很长,则按钮位于可见区域之外。

我需要在表格下方(以及表格内部)动态创建一个按钮

我尝试使用下面的代码(以及其他代码):

void addFooder() {
    /** Create a TableRow dynamically **/
    tr = new TableRow(this);
    /** Creating a TextView to add to the row **/
    myButton = new Button(this);
    myButton.setText("Send");
    myButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));

    LinearLayout Ll3 = new LinearLayout(this);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    params.setMargins(5, 5, 5, 5);
    // Ll.setPadding(10, 5, 5, 5);
    Ll3.addView(label, params);
    tr.addView((View) Ll3); // Adding textView to tablerow.
    // Add the TableRow to the TableLayout
    tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));


}

logcat的:

java.lang.RuntimeException: Unable to start activity ComponentInfo{eu.straff/eu.straff.OutAcceptActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

0 个答案:

没有答案