Android ExpandableListView中的线性布局
我想对ExpandableChildView组件的childView进行充气 代码: 其中linearOpt是一个向量,它包含我实例化的许多LinearLayout对象Android ExpandableListView中的线性布局,android,android-linearlayout,expandablelistview,inflate,Android,Android Linearlayout,Expandablelistview,Inflate,我想对ExpandableChildView组件的childView进行充气 代码: 其中linearOpt是一个向量,它包含我实例化的许多LinearLayout对象 private void prepareThemes(){ View v = mInflater.inflate(R.layout.widget_configure, null); LinearLayout theme = (LinearLayout) v.findViewById(R.i
private void prepareThemes(){
View v = mInflater.inflate(R.layout.widget_configure, null);
LinearLayout theme = (LinearLayout) v.findViewById(R.id.themeLayout);
LinearLayout color = (LinearLayout) v.findViewById(R.id.colorLayout);
LinearLayout trans = (LinearLayout) v.findViewById(R.id.transpLayout);
themesOptions.add(theme);
themesOptions.add(color);
themesOptions.add(trans);
}
这是R.layout.itemrow xml:
但我收到了这个错误:
07-18 10:48:49.740:
错误/AndroidRuntime2738:
IllegalStateException:
指定的子级已具有父级。
您必须在上调用removeView
首先是孩子的父母
如何解决此问题?我认为问题是由您在prepareThemes中准备布局的方式引起的 您没有提到,但我假设您的“layout/widget_configure.xml”定义了如下结构:
<LinearLayout android:id="@+id/root">
<LinearLayout android:id="@+id/themeLayout> <!-- ... --> </LinearLayout>
<LinearLayout android:id="@+id/colorLayout> <!-- ... --> </LinearLayout>
<LinearLayout android:id="@+id/transpLayout> <!-- ... --> </LinearLayout>
</LinearLayout>
然后在准备阶段将其充气,得到3个子布局。但在这一刻,它们已经注册为我称之为根的周围布局的子级。正如错误所暗示的,视图实例只能添加到一个父级
您可以将每个LinearLayout存储在自己的xml文件中,然后充气3次。然后,这些存储的布局可以只添加一次到子级
我不确定您想做什么,但我想,与其准备,不如只使用3种不同的布局,其中包括layout.itemrow中的部分,然后在getChildView中创建一个切换框,并动态膨胀所需的布局。因为即使在prepareThemes中存储一个非绑定布局:一旦有多个组,在将预存储布局添加到snd组的子级时,也会出现相同的错误
我希望这有帮助
<LinearLayout android:id="@+id/root">
<LinearLayout android:id="@+id/themeLayout> <!-- ... --> </LinearLayout>
<LinearLayout android:id="@+id/colorLayout> <!-- ... --> </LinearLayout>
<LinearLayout android:id="@+id/transpLayout> <!-- ... --> </LinearLayout>
</LinearLayout>