Java 如何保留XML中的组件和通过编程添加的组件

Java 如何保留XML中的组件和通过编程添加的组件,java,android,Java,Android,我是Android新手 我正在使用Eclipse中的拖放功能绘制文本字段 还可以通过编程方式添加表 但我只能看到通过编程添加的表 如何保留XML中的组件和通过编程添加的组件 对不起,如果这是一个非常基本和愚蠢的问题 这是我的密码 <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android

我是Android新手

我正在使用Eclipse中的拖放功能绘制文本字段

还可以通过编程方式添加表

但我只能看到通过编程添加的表

如何保留XML中的组件和通过编程添加的组件

对不起,如果这是一个非常基本和愚蠢的问题

这是我的密码

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:shrinkColumns="*"
    android:stretchColumns="*">

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <ZoomButton
        android:id="@+id/zoomButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/btn_plus" />

    <ZoomControls
        android:id="@+id/zoomControls1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TwoLineListItem
        android:id="@+id/twoLineListItem1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <AbsoluteLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </AbsoluteLayout>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPassword" />

    <AnalogClock
        android:id="@+id/analogClock1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <CalendarView
        android:id="@+id/calendarView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</TableLayout>

但是我如何合并它呢?

在下面的活动中键入oncreate

TableLayout tbl_lay=(TableLayout)findViewById(R.id.tablelayout1);
.....
.....
.....
Your Code.....
TableLayout tb=new TableLayout(this);
.....
.....
至少添加这些行

tbl_lay.addview(tb);
它将帮助您

您的布局名称是xml中的“tableLayout1”,您应该使用findViewById从中实现TableLayout对象,并向其中添加以编程方式创建的视图。请注意定位,您可能会将其放在拖放视图上

TableLayout myLayout = (TableLayout) findViewById(R.id.tableLayout1);
//your elements created
myLayout.addView(yourCreatedViewElement);

你能帮我写下密码吗?但我一次只能用一个对吗
setContentView(table)
setContentView(R.id.tableLayout1)
如何添加两者?您可以随时使用setContentView(anyView),但无论何时使用,您都会使用新的布局更改所看到的布局。您应该设置ContentView(您的XMLCreatedView),然后像我写的那样将其主布局放到一个对象上。之后,您可以向其中添加以编程方式创建的视图,无需再次使用setContentView。
tbl_lay.addview(tb);
TableLayout myLayout = (TableLayout) findViewById(R.id.tableLayout1);
//your elements created
myLayout.addView(yourCreatedViewElement);