6

我的应用程序活动有以下 xml 结构。现在我想用 id layer1Front 以编程方式删除子 RelativeLayout。我将如何在代码中做到这一点。我不想隐藏它,由于我的应用程序中的内存问题,我需要将其删除。同样在以某种方式删除它之后,我的应用程序会比当前的应用程序更轻、更快吗?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/layer1Front" >
    </RelativeLayout>   
    <HorizontalScrollView android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <FrameLayout android:layout_width="wrap_content"
            android:layout_height="fill_parent"                   
            android:id="@+id/parallaxLayers"        
            android:visibility="gone">      
        </FrameLayout>
    </HorizontalScrollView>
    <RelativeLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/frontView">

    </RelativeLayout>       

</RelativeLayout>
4

2 回答 2

31

最简单的是

findViewById(R.id.layer1front).setVisibility(View.GONE);

但是你也可以有类似的东西

View root = findViewById(R.id.your_root);
root.removeView(yourViewToRemove);

不,您的应用在删除后不会变得更轻或更快

于 2012-04-18T14:24:56.307 回答
2

尝试获取父布局而不是删除子布局

parentView.remove(child) 

我希望这行得通。

于 2012-04-18T14:21:14.963 回答