如何动态添加XML副本

时间:2014-01-02 02:15:47

标签: java android xml android-layout

我正在制作Android应用,我想在线性布局中复制一些XML代码,然后将其重新插入线性布局,以便线性布局中有两个相对布局。我想通过以下代码动态地执行此操作:

 <LinearLayout
        android:id="@+id/tileContainerME"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <RelativeLayout
        android:id="@+id/tilesAreHERE"
        android:layout_width="207dp"
        android:layout_height="151dp" >

        <TextView
            android:id="@+id/bottom1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/top1"
            android:layout_alignParentBottom="true"
            android:gravity="center_horizontal"
            android:text="1"
            />

        <TextView
            android:id="@+id/left1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:gravity="center_vertical"
            android:text="2"
             />

        <TextView
            android:id="@+id/right1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/left1"
            android:layout_alignBottom="@+id/left1"
            android:layout_alignParentRight="true"
            android:gravity="center_vertical"
            android:text="3"
           />

        <TextView
            android:id="@+id/top1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:gravity="center_horizontal"
            android:text="4"
             />

    </RelativeLayout>            
    </LinearLayout>

然后简单地把它变成这个:

 <LinearLayout
        android:id="@+id/tileContainerME"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <RelativeLayout
        android:id="@+id/tilesAreHERE"
        android:layout_width="207dp"
        android:layout_height="151dp" >

        <TextView
            android:id="@+id/bottom1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/top1"
            android:layout_alignParentBottom="true"
            android:gravity="center_horizontal"
            android:text="1"
            />

        <TextView
            android:id="@+id/left1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:gravity="center_vertical"
            android:text="2"
             />

        <TextView
            android:id="@+id/right1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/left1"
            android:layout_alignBottom="@+id/left1"
            android:layout_alignParentRight="true"
            android:gravity="center_vertical"
            android:text="3"
           />

        <TextView
            android:id="@+id/top1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:gravity="center_horizontal"
            android:text="4"
             />

    </RelativeLayout>  

        <RelativeLayout
        android:id="@+id/tilesAreHERE"
        android:layout_width="207dp"
        android:layout_height="151dp" >

        <TextView
            android:id="@+id/bottom1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/top1"
            android:layout_alignParentBottom="true"
            android:gravity="center_horizontal"
            android:text="1"
            />

        <TextView
            android:id="@+id/left1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:gravity="center_vertical"
            android:text="2"
             />

        <TextView
            android:id="@+id/right1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/left1"
            android:layout_alignBottom="@+id/left1"
            android:layout_alignParentRight="true"
            android:gravity="center_vertical"
            android:text="3"
           />

        <TextView
            android:id="@+id/top1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:gravity="center_horizontal"
            android:text="4"
             />

    </RelativeLayout>                 
    </LinearLayout>

看看会有两个RelativeLayout部分...我想基本上复制一个然后再添加它(我真的不知道在我的程序中有多少次我可能需要这样做,这就是为什么我不是将它直接插入XML中,我想从Java代码中实现它。 这是我到目前为止所做的,但每当我运行它时,布局都是错误的。我的代码可能有什么问题?

            LinearLayout m3 = (LinearLayout)findViewById(R.id.tileContainerME);
    RelativeLayout m = (RelativeLayout)findViewById(R.id.tilesAreHERE);     
    RelativeLayout m2 = new RelativeLayout(this);

    m2.setLayoutParams(m.getLayoutParams());
    m2.setGravity(m.getGravity());
    m2.setLayoutDirection(m.getLayoutDirection());


    TextView et1 = (TextView) findViewById(R.id.bottom1);
    TextView et2 = (TextView) findViewById(R.id.left1);
    TextView et3 = (TextView) findViewById(R.id.right1);
    TextView et4 = (TextView) findViewById(R.id.top1);

    TextView tv1 = new TextView(et1.getContext());
    TextView tv2 = new TextView(et2.getContext());
    TextView tv3 = new TextView(et3.getContext());
    TextView tv4 = new TextView(et4.getContext());

    tv1.setLayoutDirection(et1.getLayoutDirection());
    tv2.setLayoutDirection(et2.getLayoutDirection());
    tv3.setLayoutDirection(et3.getLayoutDirection());
    tv4.setLayoutDirection(et4.getLayoutDirection());

    tv1.setGravity(et1.getGravity());
    tv2.setGravity(et2.getGravity());
    tv3.setGravity(et3.getGravity());
    tv4.setGravity(et4.getGravity());

    tv1.setText(et1.getText());
    tv2.setText(et2.getText());
    tv3.setText(et3.getText());
    tv4.setText(et4.getText());

    m2.addView(tv4,et4.getLayoutParams());
    m2.addView(tv3,et3.getLayoutParams());
    m2.addView(tv2,et2.getLayoutParams());
    m2.addView(tv1,et1.getLayoutParams());  

    m3.addView(m2);

我没有看到我的代码有什么问题,任何建议......

这是完整的XML数据文件:

         <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
  <HorizontalScrollView
        android:id="@+id/scrollHORIZON"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/linearLayout1" >



        <LinearLayout
            android:id="@+id/tileContainerME"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <RelativeLayout
            android:id="@+id/tilesAreHERE"
            android:layout_width="207dp"
            android:layout_height="151dp" >

            <TextView
                android:id="@+id/bottom1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/top1"
                android:layout_alignParentBottom="true"
                android:gravity="center_horizontal"
                android:text="1"
                />

            <TextView
                android:id="@+id/left1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:gravity="center_vertical"
                android:text="2"
                 />

            <TextView
                android:id="@+id/right1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@+id/left1"
                android:layout_alignBottom="@+id/left1"
                android:layout_alignParentRight="true"
                android:gravity="center_vertical"
                android:text="3"
               />

            <TextView
                android:id="@+id/top1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:gravity="center_horizontal"
                android:text="4"
                 />

        </RelativeLayout>           
        </LinearLayout>


    </HorizontalScrollView>

</RelativeLayout>

4 个答案:

答案 0 :(得分:0)

您可能收到一条错误消息,指出您尝试添加的View已有父级。您需要在removeView()TextView的父级上致电removeAllViews(),然后将其全部删除,然后再将其添加到新的RelativeLayout。像

这样的东西
m.removeAllViews();

m2.addView(et1);
m2.addView(et2);
m2.addView(et3);
m2.addView(et4);

但是,如果您要多次添加它们,那么您可能想要像使用RelativeLayoutm2)一样创建新实例。然后,您可以使用原始params所拥有的TextView。如果这不是您当前的错误,那么请发布您的logcat,但这会导致异常。

修改

TextView et1 = (TextView) findViewById(R.id.top1);
TextView et2 = (TextView) findViewById(R.id.right1);
TextView et3 = (TextView) findViewById(R.id.left1);
TextView et4 = (TextView) findViewById(R.id.bottom1);

//Create the new TextViews
TextView tv1 = new TextView(m.getContext());  //if inside Activity you can use this instead of m.getContext()
// set params which you can get from the above TextViews
m2.addView(tv1);
...

答案 1 :(得分:0)

我明白了!

   LinearLayout m3 = (LinearLayout)findViewById(R.id.tileContainerME);
    RelativeLayout m = (RelativeLayout)findViewById(R.id.tilesAreHERE);     
    RelativeLayout m2 = new RelativeLayout(m.getContext());

    m2.setLayoutParams(m.getLayoutParams());


    TextView et1 = (TextView) findViewById(R.id.top1);
    TextView et2 = (TextView) findViewById(R.id.right1);
    TextView et3 = (TextView) findViewById(R.id.left1);
    TextView et4 = (TextView) findViewById(R.id.bottom1);

    TextView tv1 = new TextView(et1.getContext());
    TextView tv2 = new TextView(et2.getContext());
    TextView tv3 = new TextView(et3.getContext());
    TextView tv4 = new TextView(et4.getContext());
    tv1.setText(et1.getText());
    tv2.setText(et2.getText());
    tv3.setText(et3.getText());
    tv4.setText(et4.getText());
    tv1.setLayoutParams(et1.getLayoutParams());
    tv2.setLayoutParams(et2.getLayoutParams());
    tv3.setLayoutParams(et3.getLayoutParams());
    tv4.setLayoutParams(et4.getLayoutParams());

    m2.addView(tv1);
    m2.addView(tv2);
    m2.addView(tv3);
    m2.addView(tv4);

    m3.addView(m2);

这将复制先前的布局,然后将其添加回当前布局!

答案 2 :(得分:0)

再次在其中放置相同的布局可能有助于您实现这一目标。

试试这段代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);

    LinearLayout main_layout = (LinearLayout)findViewById(R.id.tileContainerME);
    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout child = (LinearLayout) inflater.inflate(R.layout.main_layout, null);
    main_layout.addView(child, 1);
}

你会得到这样的东西:

enter image description here

答案 3 :(得分:0)

我确定这次工作了!我用了这段代码:

 LinearLayout item = (LinearLayout)findViewById(R.id.tileContainerME);
        View child = getLayoutInflater().inflate(R.layout.activity_main, null);
        item.addView(child);

它将复制'activity_main'类,该类仅包含tile的XML数据。然后将其复制到水平滚动视图中!以下是我的解决方案的屏幕截图:

http://i.stack.imgur.com/WxoJG.png