android linearlayout动态添加view 怎么清空
展开全部
private LinearLayout layout;
private TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = new LinearLayout( this ); // 变量layout是该Activity的成员变量(private LinearLayout layout)
layout.setOrientation( LinearLayout.VERTICAL ); // 设置layout布局方向为垂直
setContentView( layout );
// 接下来向layout中添加TextView
textView = new TextView( this );
textView.setText( "This Is a TextView" );
layout.addView( textView );
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
layout.removeView(textView);
super.onResume();
}
}
但是Activity在启动的时候调用onCreate()之后也会调用onResume()方法,所以进入程序也看不到textview
private TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = new LinearLayout( this ); // 变量layout是该Activity的成员变量(private LinearLayout layout)
layout.setOrientation( LinearLayout.VERTICAL ); // 设置layout布局方向为垂直
setContentView( layout );
// 接下来向layout中添加TextView
textView = new TextView( this );
textView.setText( "This Is a TextView" );
layout.addView( textView );
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
layout.removeView(textView);
super.onResume();
}
}
但是Activity在启动的时候调用onCreate()之后也会调用onResume()方法,所以进入程序也看不到textview
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询