简体   繁体   中英

add linearlayout dynamically into a Relativelayout

I have this code and i want to add CheckBoxes dynamically inside a LinearLayout that nested inside a ScrollView that nested inside a RelativeLayout( RelativeLayout->ScrollView->LinearLayout->My ChechBoxes)

li = (RelativeLayout) findViewById(R.id.mainlayout);    
ScrollView sv = new ScrollView(this);
final LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
li.addView(sv);
sv.addView(ll);
for(int i = 0; i < 20; i++) {
    CheckBox cb = new CheckBox(getApplicationContext());
    cb.setText("I'm dynamic!");
    ll.addView(cb);
}
this.setContentView(sv);

but i get this error:

03-12 20:32:14.840: E/AndroidRuntime(945): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

My RelativeLayout declared in my XML file already how i can fix this?

this.setContentView(sv);

This tries to add your ScrollView to the FrameLayout android.R.id.content , but you already made li the parent of sv ... hence "The specified child already has a parent."

I believe you can remove this.setContentView(sv); since it looks like you only want to add the ScrollView (et al.) to the RelativeLayout, not replace the entire existing layout.

Check this http://developer.android.com/training/animation/screen-slide.html When you download the sample app, go through LayoutChangesActivity.java

The following is the code to add an item..

private void addItem() {
    // Instantiate a new "row" view.
    final ViewGroup newView = (ViewGroup) LayoutInflater.from(this).inflate(
            R.layout.list_item_example, mContainerView, false);

    // Set the text in the new row to a random country.
    ((TextView) newView.findViewById(android.R.id.text1)).setText(
            COUNTRIES[(int) (Math.random() * COUNTRIES.length)]);

    // Set a click listener for the "X" button in the row that will remove the row.
    newView.findViewById(R.id.delete_button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Remove the row from its parent (the container view).
            // Because mContainerView has android:animateLayoutChanges set to true,
            // this removal is automatically animated.
            mContainerView.removeView(newView);

            // If there are no rows remaining, show the empty view.
            if (mContainerView.getChildCount() == 0) {
                findViewById(android.R.id.empty).setVisibility(View.VISIBLE);
            }
        }
    });

    // Because mContainerView has android:animateLayoutChanges set to true,
    // adding this view is automatically animated.
    mContainerView.addView(newView, 0);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM