2014-09-02 2 views
0

Я пытаюсь добавить linearlayout 2 ["secondrow"] в linearlayout 1 ["firstrow"], но он выдает исключение Указанный ребенок уже имеет родителя, вы должны вызвать removeview() на родительском родителе first.lingleayout1, linearlayout 2, linearlayout 3, linearlayout 4 имеет 4 кнопки каждый. Мой код java нижеДобавление линейной компоновки 2 к линейной компоновке 1

--------------------- ------- Java код --------------------------

private LinearLayout linearfirstRow; 
     private LinearLayout linearsecondRow; 
     private LinearLayout linearthirdRow; 
     private LinearLayout linearfourthRow; 
     private LinearLayout TemporaryLayout; 
     linearfirstRow=(LinearLayout)findViewById(R.id.firstrow); 
     linearsecondRow=(LinearLayout)findViewById(R.id.secondrow); 
     linearthirdRow=(LinearLayout)findViewById(R.id.thirdrow); 
     linearfourthRow=(LinearLayout)findViewById(R.id.fourththrow); 
       TemporaryLayout=linearsecondRow; 
          linearfirstRow.removeAllViews(); 
          linearsecondRow.removeAllViews(); 
      Throws error ---> linearfirstRow.addView(TemporaryLayout); 

   <?xml version="1.0" encoding="utf-8"?> 
       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:orientation="vertical" > 
        <TextView 
          android:id="@+id/textView1" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_weight="1" 
          android:text="Medium Text" 
          android:textAppearance="?android:attr/textAppearanceMedium" /> 
          <LinearLayout 
      firstrow--> android:id="@+id/firstrow" 
         android:layout_width="match_parent" 
         android:layout_height="0dp" 
         android:layout_weight="25" 
         > 
         <Button 
          android:id="@+id/btn1" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:text="1" 
          android:layout_weight="1" /> 

         <Button 
          android:id="@+id/btn2" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:text="1" 
          android:layout_weight="1" /> 

         <Button 
          android:id="@+id/btn3" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:text="1" 
          android:layout_weight="1" /> 
         <Button 
          android:id="@+id/btn4" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:text="1" 
          android:layout_weight="1" /> 
        </LinearLayout> 
     secondrow-->  <LinearLayout 
         android:id="@+id/secondrow" 
         android:layout_width="match_parent" 
         android:layout_height="0dp" 
         android:layout_weight="25" 
         > 

         <Button 
          android:id="@+id/btn5" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:text="2" 
          android:layout_weight="1" /> 

         <Button 
          android:id="@+id/btn6" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:text="2" 
          android:layout_weight="1" /> 
         <Button 
          android:id="@+id/btn7" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:text="2" 
          android:layout_weight="1" /> 
         <Button 
          android:id="@+id/btn8" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:text="2" 
          android:layout_weight="1" /> 
        </LinearLayout> 
        <LinearLayout 
         android:id="@+id/thirdrow" 
         android:layout_width="match_parent" 
         android:layout_height="0dp" 
         android:layout_weight="25" > 

         <Button 
          android:id="@+id/btn9" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:text="3" 
          android:layout_weight="1" /> 

         <Button 
          android:id="@+id/btn10" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:text="3" 
          android:layout_weight="1" /> 
         <Button 
          android:id="@+id/btn11" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:text="3" 
          android:layout_weight="1" /> 
         <Button 
          android:id="@+id/btn12" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:text="3" 
          android:layout_weight="1" /> 
        </LinearLayout> 
        <LinearLayout 
         android:id="@+id/fourththrow" 
         android:layout_width="match_parent" 
         android:layout_height="0dp" 
         android:layout_weight="25"> 

         <Button 
          android:id="@+id/btn13" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:text="4" 
          android:layout_weight="1" /> 

         <Button 
          android:id="@+id/btn14" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:text="4" 
          android:layout_weight="1" /> 
         <Button 
          android:id="@+id/btn15" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:text="4" 
          android:layout_weight="1" /> 
         <Button 
          android:id="@+id/btn16" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:text="4" 
          android:layout_weight="1" /> 
        </LinearLayout> 

       </LinearLayout> 

ответ

0

Вы должны отделить TemporaryLayout/linearsecondRow от своего родителя LinearLayout первого т.е.

 private LinearLayout linearfirstRow; 
     private LinearLayout linearsecondRow; 
     private LinearLayout linearthirdRow; 
     private LinearLayout linearfourthRow; 
     private LinearLayout TemporaryLayout; 
     linearfirstRow=(LinearLayout)findViewById(R.id.firstrow); 
     linearsecondRow=(LinearLayout)findViewById(R.id.secondrow); 
     linearthirdRow=(LinearLayout)findViewById(R.id.thirdrow); 
     linearfourthRow=(LinearLayout)findViewById(R.id.fourththrow); 
       TemporaryLayout=linearsecondRow; 
          linearfirstRow.removeAllViews(); 
          linearsecondRow.removeAllViews(); 

//Add this although i dont know why the above two lines were used by you 
     ((LinearLayout)TemporaryLayout.getParent()).removeView(TemporaryLayout); 
     linearfirstRow.addView(TemporaryLayout); 
+0

Спасибо у получил его. –

+0

Пожалуйста, отметьте это как ответ и голосуйте – humblerookie

Смежные вопросы