首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将线性布局2添加到线性布局1

将线性布局2添加到线性布局1
EN

Stack Overflow用户
提问于 2014-09-02 03:55:08
回答 1查看 50关注 0票数 0

我试图将线性布局2“第二行”添加到线性布局1“第一行”,但它会抛出一个异常--指定的子程序已经有一个父程序,您必须在子父first.linearlayout1上调用远程视图(),线性布局2,线性布局3,线性布局4有4个按钮,each.My java代码如下

代码语言:javascript
复制
    private LinearLayout linearfirstRow;
        private LinearLayout linearsecondRow;
        private LinearLayout linearthirdRow;
        private LinearLayout linearfourthRow;
        private LinearLayout TemporaryLayout;
        linearfirstRow=(LinearLayout)findViewById(R.id.firstrow);
        linearsecondRow=(LinearLayout)findViewById(R.id.secondrow);
        linearthirdRow=(LinearLayout)findViewById(R.id.thirdrow);
        linearfourthRow=(LinearLayout)findViewById(R.id.fourththrow);
              TemporaryLayout=linearsecondRow;
                            linearfirstRow.removeAllViews();
                            linearsecondRow.removeAllViews();
          Throws error ---> linearfirstRow.addView(TemporaryLayout);

代码语言:javascript
复制
             <?xml version="1.0" encoding="utf-8"?>
                <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical" >
                     <TextView
                            android:id="@+id/textView1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:text="Medium Text"
                            android:textAppearance="?android:attr/textAppearanceMedium" />
                           <LinearLayout
             firstrow-->   android:id="@+id/firstrow"
                        android:layout_width="match_parent"
                        android:layout_height="0dp" 
                        android:layout_weight="25"
                         >
                        <Button
                            android:id="@+id/btn1"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:text="1"
                            android:layout_weight="1" />

                        <Button
                            android:id="@+id/btn2"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:text="1"
                            android:layout_weight="1" />

                        <Button
                            android:id="@+id/btn3"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:text="1"
                            android:layout_weight="1" />
                        <Button
                            android:id="@+id/btn4"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:text="1"
                            android:layout_weight="1" />
                    </LinearLayout>
         secondrow-->       <LinearLayout
                        android:id="@+id/secondrow"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="25"
                         >

                        <Button
                            android:id="@+id/btn5"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:text="2"
                            android:layout_weight="1" />

                        <Button
                            android:id="@+id/btn6"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:text="2"
                            android:layout_weight="1" />
                        <Button
                            android:id="@+id/btn7"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:text="2"
                            android:layout_weight="1" />
                        <Button
                            android:id="@+id/btn8"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:text="2"
                            android:layout_weight="1" />
                    </LinearLayout>
                    <LinearLayout
                        android:id="@+id/thirdrow"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"  
                        android:layout_weight="25" >

                        <Button
                            android:id="@+id/btn9"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:text="3"
                            android:layout_weight="1" />

                        <Button
                            android:id="@+id/btn10"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:text="3"
                            android:layout_weight="1" />
                        <Button
                            android:id="@+id/btn11"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:text="3"
                            android:layout_weight="1" />
                        <Button
                            android:id="@+id/btn12"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:text="3"
                            android:layout_weight="1" />
                    </LinearLayout>
                    <LinearLayout
                        android:id="@+id/fourththrow"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"  
                        android:layout_weight="25">

                        <Button
                            android:id="@+id/btn13"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:text="4"
                            android:layout_weight="1" />

                        <Button
                            android:id="@+id/btn14"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:text="4"
                            android:layout_weight="1" />
                        <Button
                            android:id="@+id/btn15"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:text="4"
                            android:layout_weight="1" />
                        <Button
                            android:id="@+id/btn16"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:text="4"
                            android:layout_weight="1" />
                    </LinearLayout>

                </LinearLayout>
EN

回答 1

Stack Overflow用户

发布于 2014-09-02 07:35:01

您需要首先将TemporaryLayout/linearsecondRow从其父线性布局中分离出来,即

代码语言:javascript
复制
        private LinearLayout linearfirstRow;
        private LinearLayout linearsecondRow;
        private LinearLayout linearthirdRow;
        private LinearLayout linearfourthRow;
        private LinearLayout TemporaryLayout;
        linearfirstRow=(LinearLayout)findViewById(R.id.firstrow);
        linearsecondRow=(LinearLayout)findViewById(R.id.secondrow);
        linearthirdRow=(LinearLayout)findViewById(R.id.thirdrow);
        linearfourthRow=(LinearLayout)findViewById(R.id.fourththrow);
              TemporaryLayout=linearsecondRow;
                            linearfirstRow.removeAllViews();
                            linearsecondRow.removeAllViews();

//Add this although i dont know why the above two lines were used by you   
        ((LinearLayout)TemporaryLayout.getParent()).removeView(TemporaryLayout);
        linearfirstRow.addView(TemporaryLayout);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25615590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档