java - removeview() 不会删除 android

标签 java android android-layout layout hide

我有一个布局(主),当我单击按钮时,它会显示带有 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/

相关文章:

ArrayList 上的 JAVA SimpleDateFormat 更改日期

java - map 中键的类型不匹配:预期的org.apache.hadoop.io.Text,当我使用.txt文件作为输入时,收到的org.apache.hadoop.io.LongWritable

android - 自定义 DialogFragment 中的 View.OnClickListener

android - 仅当添加 AdMob 时应用程序在首次打开时崩溃,但在下次打开时运行正常

android - 如何禁用 RecyclerView 滚动以使布局监听其 ScrollView 父级?

java - 使用 ROW_NUMBER() 将复杂的 DB2 SQL 转换为 mongo 查询

java - 如何设置最大 xmx 和 xms 值而不出现内存不足异常

android - Android 设备上的 DXT5 纹理中缺少 Alpha channel

android - 没有长触摸,WebView 不会在某些设备上滚动

android - LinearLayout中imageView的大小