Android 以编程方式删除布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10211252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove layout programmatically
提问by Ayaz Alavi
I have got following xml structure of my app activity. Now I would like to remove child RelativeLayout programmatically with id layer1Front. How would I do that in code. I dont want to hide it, I need to remove it because of memory issues in my app. Also after removing it somehow will my app be lighter and faster than current one?
我有我的应用程序活动的以下 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>
回答by Alexander Kulyakhtin
Simplest would be
最简单的是
findViewById(R.id.layer1front).setVisibility(View.GONE);
But then you can also have something like
但是你也可以有类似的东西
View root = findViewById(R.id.your_root);
root.removeView(yourViewToRemove);
No, your app is not going to be lighter or faster after removing it
不,您的应用在移除后不会变轻或变快
回答by ValayPatel
Try fetching parent layout and than remove child
尝试获取父布局而不是删除子布局
parentView.remove(child)
I hope this works.
我希望这有效。