早教吧 育儿知识 作业答案 考试题库 百科 知识分享

使用addView是不是有什么特殊要点?为什么不能连续添加?Buttonbb=(Button)findViewById(R.id.addview);LayoutInflaterli=(LayoutInflater)this.getSystemService(Context.LAYOUTINFLATERSERVICE);resu=li.inflate(R.layout.ttt,nul

题目详情
使用addView是不是有什么特殊要点?为什么不能连续添加?
Button bb = (Button)findViewById(R.id.addview) ; LayoutInflater li = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); resu = li.inflate( R.layout.ttt, null) ; //ttt是一个简单的LinearLayout,里面就只有一个TextVieww lin = (LinearLayout)findViewById(R.id.addViewLinearLayout) ; //这个里面就一个Button,就上面那个. bb.setOnClickListener(new OnClickListener() { public void onClick(View v) { lin.addView(resu) ; } }) ;一段简单的代码,我按一下就可以添加,按第二下就崩溃了.
▼优质解答
答案和解析
这个问题呀,给你个通俗的解释吧,也是我的个人体会:给你一个苹果,你吃到肚子里了,你还想吃原来的那个,你觉得可能么.当然,有可能:1、你吐出来(removeView掉),才能再次吃到吧;2、你从新拿一个苹果(resu = li.inflate( R.layout.ttt,null) ; 放到onClick事件里面,每次都重新创建对象).查看原帖>>