当前位置: 代码迷 >> Android >> 使用setContentView后销毁布局
  详细解决方案

使用setContentView后销毁布局

热度:59   发布时间:2023-08-04 10:57:13.0

在一个按钮上,我这样调用布局:

setContentView(R.layout.my_form);

另一方面,我想破坏这种布局。 我该怎么做 ?

你可以使用rootView.removeAllViews(); 例如,从布局中删除所有视图

您的布局是

             <LinearLayout
                android:id="@+id/llRootView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                             .......

             </LinearLayout>

然后,您可以调用llRootView.removeAllViews(); 从此线性布局中删除所有视图

编辑

要从布局中临时删除视图,您可以使用

llRootView.setVisibility(View.GONE);

要使其再次可见,请使用

llRootView.setVisibility(View.VISIBLE);