2014-08-30 190 views
-3
LinearLayout linContact = (LinearLayout) mView.findViewById(R.id.linContacts); 

LinearLayout.LayoutParams leftGravityparas = new LinearLayout.LayoutParams(0,LayoutParams.WRAP_CONTENT); 
LinearLayout.LayoutParams rightGravityParams = new LinearLayout.LayoutParams(30, 30); 

for (int i = 0; i < contactList.size(); i++) { 
    final ClsAdviserData contact = .contactList.get(i); 

    if (contact.isSelected()) { 

     linearLayout = new LinearLayout(getActivity()); 
     LinearLayout.LayoutParams linMainparam = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
     linearLayout.setBackgroundColor(getActivity().getResources().getColor(R.color.light_grey_backgeound)); 
     linearLayout.setOrientation(LinearLayout.HORIZONTAL); 
     linearLayout.setLayoutParams(linMainparam); 
     linMainparam.setMargins(0, 10, 0, 0); 

     leftGravityparas.gravity = Gravity.LEFT; 
     leftGravityparas.weight = 0.9f; 
     TextView txtContact = new TextView(getActivity()); 
     txtContact.setTextSize(16); 
     // txtContact.setBackgroundColor(getActivity().getResources().getColor(R.color.light_grey_backgeound)); 
     txtContact.setLayoutParams(leftGravityparas); 
     txtContact.setId(i); 

     leftGravityparas.setMargins(0, 10, 0, 0); 

     txtContact.setPadding(20, 10, 10, 10); 
     txtContact.setText(contact.getName()); 

     linearLayout.addView(txtContact, leftGravityparas); 

     rightGravityParams.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL; 
     rightGravityParams.weight = 0.1f; 

     final ImageView imgDelContact = new ImageView(getActivity()); 

     imgDelContact.setLayoutParams(rightGravityParams); 
     imgDelContact.setTag(i); 
     imgDelContact.setClickable(true); 
     imgDelContact.setOnClickListener(this); 
     imgDelContact.setImageResource(R.drawable.ic_close_grey); 

     linearLayout.addView(imgDelContact, rightGravityParams); 
     // linContact.setTag(i); 
     linContact.addView(linearLayout); 

     imgDelContact.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Toast.makeText(v.getContext(), "Toast ==>" + contact.getName() + v.getTag(), Toast.LENGTH_SHORT).show(); 

       // linContact.removeViewAt((Integer) v.getTag()); 
       linearLayout.setVisibility(View.GONE); 

       // lin.removeViewAt((Integer)v.getTag()); 
      } 
     }); 
    } 
} 

我寫上面的代碼來創建文本框和按鈕動態;但現在我需要刪除2個文本框和一個按鈕,當點擊按鈕時。我怎麼做?如何從線性佈局動態添加和刪除元素?

+0

[動態添加和使用Java代碼刪除視圖(http://www.mysamplecode.com/2011/10/android-dynamic-layout - 使用的XML-add.html) – SilentKiller 2014-08-30 09:37:39

回答

2

加入 -
使用的LinearLayout聲明addView()方法的初始化添加子視圖後

linearLayout.addView(txtContact); 
linearLayout.addView(imgDelContact); 

隱藏 -
要隱藏查看,這樣就可以在需要時再次得到它

imgDelContact.setVisibility(View.GONE); 
txtContact.setVisibility(View.GONE); 

刪除 -
或者你可以刪除,如果你不想再使用它。

linearLayout.removeView(txtContact); 
linearLayout.removeView(imgDelContact); 
+0

,可能是工作,但它不是好的做法是肯定的。 – Gumbo 2014-08-30 09:39:03

+0

@Gumbo如果我需要使用相同的視圖後刪除它更好地隱藏它。 – SilentKiller 2014-08-30 09:41:10

+0

@Archit請提供*說明爲什麼和在哪裏使用*。 – SilentKiller 2014-08-30 09:41:51

1

要刪除您可以使用任何視圖

aLinearLayout.removeView(view)// to remove particular view 
aLinearLayout.removeViewAt(position);// to remove view from particular position