【发布时间】:2014-09-16 20:42:52
【问题描述】:
我目前正在尝试扩充一个布局,我最终将添加到一个垂直的 LinearLayout 容器中,作为更大的动态表单创建模块的一部分。
layout_checkbox.xml 如下:
<TextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="sample text" />
<LinearLayout
android:id="@+id/checkboxGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<CheckBox
android:id="@+id/checkbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="sample" />
</LinearLayout>
如果可能的话,现在我想做的是获取复选框 (@+id/checkbox) 并创建它的副本,所有这些都在 LinearLayout @+id/checkboxGroup 下。
layout = (LinearLayout) inflater.inflate(R.layout.layout_checkbox, container, false);
LinearLayout checkboxGroup = (LinearLayout) layout.findViewById(R.id.checkboxGroup);
CheckBox checkbox = (CheckBox) checkboxGroup.findViewById(R.id.checkbox);
// code that supposedly clones/duplicates checkbox.
我知道我可以轻松地从另一个 xml 定义中扩充 Checkbox 以创建它的多个实例,然后将它们添加到组中。我尝试这样做的原因是因为我正在创建某种类型的库,并且我正在尝试强制执行某种约定以使其更易于使用。
编辑:
这行得通,但它很容易闻到性能下降的味道:
layout = (LinearLayout) inflater.inflate(R.layout.layout_checkbox, container, false);
LinearLayout originalCheckboxGroup = (LinearLayout) layout.findViewById(R.id.checkboxGroup);
for (String entryItem : entry.fieldEntries) {
LinearLayout tempCheckboxLayout = (LinearLayout) inflater.inflate(R.layout.layout_checkbox, container, false);
LinearLayout checkboxGroup = (LinearLayout) tempCheckboxLayout.findViewById(R.id.checkboxGroup);
CheckBox checkbox = (CheckBox) checkboxGroup.findViewById(R.id.checkbox);
checkbox.setText(entryItem);
checkboxGroup.removeView(checkbox);
originalCheckboxGroup.addView(checkbox);
}
【问题讨论】:
标签: android android-view