我有一个布局(主),当我单击按钮时,它会显示带有 TextView 和编辑文本的相对布局(新布局)。但是当我想离开相对布局(单击某个按钮)时,它不会消失。 我的代码是这样的:
当我点击按钮时,这是代码:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_hide:
RelativeLayout main = (RelativeLayout) findViewById(R.id.atraccion_layout);
RelativeLayout newLayout= (RelativeLayout) View.inflate(this, R.layout.newLayout, null);
main.addView(newLayout);
default:
return super.onOptionsItemSelected(item);
}
}
然后,当我单击另一个按钮时,我的代码是:
public void close(View v){
RelativeLayout main = (RelativeLayout) findViewById(R.id.atraccion_layout);
RelativeLayout newLayout = (RelativeLayout) View.inflate(this, R.layout.newLayout, null);
main.removeView(comentarLayout);
main.forceLayout();
}
但是newLayout仍然存在。
也尝试过:
((RelativeLayout)v.getParent()).removeView(v);
main.removeView((View)v.getParent());
newLayout.setVisibility(v.INVISIBLE);
newLayout.setVisibility(v.GONE);
((RelativeLayout)newLayout.getParent()).forceLayout();
((RelativeLayout)newLayout.getParent()).removeView(comentarLayout);
没有成功。
有人可以帮我为什么不删除布局吗?
最佳答案
我通过使用这些层定义类属性来解决这个问题
public class ActivityClass extends Activity{
private RelativeLayout main;
private RelativeLayout newLayout;
protected void onCreate(Bundle b){...}
...
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_hide:
main = (RelativeLayout) findViewById(R.id.atraccion_layout);
newLayout= (RelativeLayout) View.inflate(this, R.layout.newLayout, null);
main.addView(newLayout);
return true;
}
default:
return super.onOptionsItemSelected(item);
}
public void close(View v){
main.removeView(newLayout);
main.forceLayout();
}
}
关于java - removeview() 不会删除 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24006100/