在片段中以编程方式将LinearLayout添加到另一个LinearLayout

时间:2014-03-18 21:17:35

标签: android fragment android-linearlayout

我完全不了解如何将LinearLayout添加到另一个LinearLayout。你可以帮我吗?我有XML文件:

   <LinearLayout
        android:id="@+id/hl"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ffdf33"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/ll"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#ffdf33"
            android:clickable="true"
            android:focusable="true"
            android:onClick="detaily"
            android:orientation="horizontal"
            android:paddingBottom="10sp" 
            >

            <TextView
                android:id="@+id/nazev"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:duplicateParentState="true"
                android:text="@string/nazev"
                android:textColor="#000000"
                android:textSize="40sp"
                android:textStyle="normal" />

            <TextView
                android:id="@+id/adresa"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10sp"
                android:duplicateParentState="true"
                android:text="@string/adresa"
                android:textColor="#000000"
                android:textSize="20sp"
                android:textStyle="normal" />
        </LinearLayout>
    </LinearLayout>

彼此内部有2个LinearLayouts。 我想添加一些ID为&#34; ll&#34;的LinearLayouts到了带有id&#34; hl&#34;的LInearLayout在Java代码中动态地。一切都在片段BTW内。

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main_dummy,
                container, false);
              LinearLayout myLayout = (LinearLayout) rootView.findViewById(R.id.hl);
              LinearLayout myLayout1 = (LinearLayout) rootView.findViewById(R.id.ll);
              myLayout.addView(myLayout1);
        return rootView;
    }

我在这里有logcat:

03-20 11:21:23.580: E/AndroidRuntime(30529): FATAL EXCEPTION: main
03-20 11:21:23.580: E/AndroidRuntime(30529): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

0 个答案:

没有答案