2014-08-30 191 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