因此,我有一段代码,我在其中单击用户的按钮即可创建textView。由于它是在运行时之后创建的,因此我不确定ID是什么。但是我想做的是可以选择删除最后添加的textView,并清除所有添加的TextView。谢谢
private TextView createNewTextView(String text)
{
ArraySize++;
final LinearLayout mLayout=findViewById(R.id.linearLayout);
String newLine=System.getProperty("line.separator");
final LinearLayout.LayoutParams lparams =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ViewGroup.LayoutParams params=(RelativeLayout.LayoutParams) mLayout.getLayoutParams();
final TextView textView=new TextView(this);
textView.setLayoutParams(lparams);
textView.setText("New texT:: "+text+newLine);
listOfNames.add(text);
return textView;
}
答案 0 :(得分:0)
如this问题中所述,您只需使用
((ViewGroup) textView.getParent()).removeView(textView);
答案 1 :(得分:0)
要删除所有视图:
mLayout.removeAllViews();
要删除最后一个TextView:
mLayout.removeView(mLayout.getChildAt(mLayout.getChildCount()-1));
答案 2 :(得分:0)
您可以在此路径中创建XML文件res / values / ids.xml;
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="view_1" type="id"/>
</resources>
然后在Java中添加ID,就像这样
textView.setId(R.id.view_1);
以及要删除它的时间
LinearLayout mLayout=findViewById(R.id.linearLayout);//to find in this specific view
TextView textView = (TextView)mLayout.findViewById(R.id.view_1);
mLayout.removeView(textView);
答案 3 :(得分:0)
您将removeViewAt
与索引
final LinearLayout mLayout=findViewById(R.id.linearLayout)
mLayout.removeViewAt(mLayout.getChildCount()-1); // get the last view
//or using index counter variable
//mLayout.removeViewAt(ArraySize-1); // adjust the value accordingly
或者您可以使用removeView
获取该视图并删除该视图final LinearLayout mLayout=findViewById(R.id.linearLayout)
View v = mLayout.getChildAt(mLayout.getChildCount()-1);
mLayout.removeView(v);