gpt4 book ai didi

java - removeview() 不会删除 android

转载 作者:行者123 更新时间:2023-11-30 02:51:50 25 4
gpt4 key购买 nike

我有一个布局(主要),当我单击一个按钮时,它会显示一个带有 TextView 和编辑文本的 RelativeLayout (newLayout)。但是当我想离开那个 RelativeLayout (点击某个按钮)时,它不会消失。我的代码是这样的:

当我点击按钮时,这是代码:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com