我想创建一个动态布局,但我有错误
指定的孩子已经有父母。您必须首先在孩子的父母身上调用removeView()。
我在Stackoverlow上发现了一些东西,但最重要的是建议使用我已经使用的这一行:
查看rootView = inflater.inflate(R.layout.fragment_item,container,false);
任何解决方案??
ItemFragment.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_items, container, false);
layout = (TableLayout) rootView.findViewById(R.id.itmes_fragment_layout);
item_folder_button = new Button(getActivity());
item_folder_button = (Button) rootView.findViewById(R.id.items_button_layout);
return rootView;
}
private void createItemsButtons() {
layout.removeAllViews();
if (itemscontainer.length == 0) {
TableRow row = new TableRow(myContext);
TextView no_items = new TextView(myContext);
no_items.setText(getResources().getString(R.string.no_items));
no_items.setTextSize(35);
row.addView(no_items);
layout.addView(row);
} else {
for (int i = 0; i <= itemscontainer.length; ) {
TableRow tableRow = new TableRow(getActivity());
TableRow.LayoutParams param = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f);
tableRow.setLayoutParams(param);
tableRow.setGravity(Gravity.CENTER_VERTICAL);
for (int y = 0; (y < 3) && (i <= itemscontainer.length); y++) {
item_folder_button.setText(("Text " + i));
item_folder_button.setVisibility(View.VISIBLE);
item_folder_button.setGravity(Gravity.CENTER);
TableRow.LayoutParams par = new TableRow.LayoutParams(y);
item_folder_button.setLayoutParams(par);
int id = i;
item_folder_button.setId(id);
item_folder_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int btn_selected = item_folder_button.getId();
Fragment fragment = new ItemFragment();
if (fragment != null) {
FragmentManager fragmentManager = myContext.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment).addToBackStack(null);
fragmentTransaction.commit();
}
}
});
tableRow.addView(item_folder_button);
i++;
}
layout.removeView(layout.getRootView());
layout.addView(tableRow);
}
}
}
return rootView;
}
fragment_items.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/items_fragment_scrollview_layout"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
android:id="@+id/items_fragment_linearlayout"
>
<TableLayout
android:id="@+id/items_fragment_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:stretchColumns="*"
android:gravity="center_vertical" />
<Button
android:id="@+id/items_button_layout"
android:layout_width="300dp"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_weight="40"
android:drawableLeft="@drawable/folder"
android:paddingLeft="10dp"
android:text="@string/text"
android:background="@drawable/item_button"
android:drawablePadding="-75dp"
android:visibility="gone"/>
</LinearLayout>