Java 无法以编程方式创建TableLayout

Java 无法以编程方式创建TableLayout,java,android,xml,layout,tablelayout,Java,Android,Xml,Layout,Tablelayout,我已经用xml创建了我想要的东西,但是我需要用编程的方式来完成,我无法让它工作。这是一个只有一行的表格布局。该行中还有两个表布局。第一个表布局有一行包含图片,第二个表布局有两行包含两个文本视图和一个图片。是否存在某种xml到java的转换器?我现在已经试着写了几个小时的代码,但无法让布局正常工作。我会发布一张照片,但我需要先获得10个代表点。如果有帮助的话,整个xml文件是线性布局的 <TableLayout android:id="@+id/tablemain"

我已经用xml创建了我想要的东西,但是我需要用编程的方式来完成,我无法让它工作。这是一个只有一行的表格布局。该行中还有两个表布局。第一个表布局有一行包含图片,第二个表布局有两行包含两个文本视图和一个图片。是否存在某种xml到java的转换器?我现在已经试着写了几个小时的代码,但无法让布局正常工作。我会发布一张照片,但我需要先获得10个代表点。如果有帮助的话,整个xml文件是线性布局的

<TableLayout
            android:id="@+id/tablemain"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:gravity="center_horizontal" >

            <TableRow
                android:id="@+id/TableRow1"
                android:background="@drawable/border"
                android:weightSum="100" >

                <TableLayout
                    android:id="@+id/Tableout2"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="0"
                    android:background="@drawable/border"
                    android:gravity="center_horizontal" >

                    <TableRow
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content" >

                        <ImageView
                            android:id="@+id/imageView1"
                            android:adjustViewBounds="true"
                            android:maxHeight="100dp"
                            android:maxWidth="100dp"
                            android:padding="3dp"
                            android:src="@drawable/unknown" />
                    </TableRow>
                </TableLayout>

                <TableLayout
                    android:id="@+id/Tableout3"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="100"
                    android:gravity="center_horizontal"
                    android:weightSum="100" >

                    <TableRow
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_weight="50"
                        android:background="@drawable/border" >

                        <TextView
                            android:id="@+id/Judgename"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:padding="7dp"
                            android:text="Name"
                            android:textSize="20sp"
                            android:textStyle="bold" />
                    </TableRow>

                    <TableRow
                        android:id="@+id/tablerowctroom"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_weight="50"
                        android:background="@drawable/border"
                        android:weightSum="100" >

                        <TextView
                            android:id="@+id/Courtroom"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_weight="70"
                            android:padding="7dp"
                            android:text="Room"
                            android:textSize="15sp"
                            android:textStyle="bold" />

                        <ImageView
                            android:id="@+id/imageView1"
                            android:layout_width="40dp"
                            android:layout_height="40dp"
                            android:layout_gravity="center_vertical"
                            android:layout_marginRight="10dp"
                            android:layout_weight="30"
                            android:src="@drawable/umid" />
                    </TableRow>
                </TableLayout>
            </TableRow>
        </TableLayout>

我想你想要的代码是这样的。让我知道它是否有意义

// Create an array of things to create views from.
// You will probably be getting your array from somewhere else
YourObjectHere[] things = new YourObjectHere[2];
things[0] = new YourObjectHere();
things[1] = new YourObjectHere();

// Get a reference to a LinearLayout to hold your views
LinearLayout list = (LinearLayout) findViewById(R.id.list);

// Loop through your list of objects
for(int i = 0; i < things.length; i++) {

    // Inflate an instance of your layout
    View listItem = getLayoutInflater().inflate(R.layout.child, null);

    // Set the TextViews to the appropriate things
    ((TextView) listItem.findViewById(R.id.name)).setText(things[i].getName());
    ((TextView) listItem.findViewById(R.id.type)).setText(things[i].getType());

    // Add the inflated view to the list
    item.addView(child);
}

如果您已经成功地在XML中创建了它,为什么要以编程方式进行呢?我需要从一个数组中生成几行。这是一个目录,因此人数可能会发生变化。您最好的选择可能是放大XML布局的多个副本,使用适当的数据初始化TextView和ImageView,然后手动将它们添加到视图组中。我以前从未使用过视图组。你有一个例子来说明你的意思吗?对不起,ViewGroup是LinearLayout和RelativeLayout等类的父类。在这种情况下,因为您可能正试图创建一个列表?您可以将这些视图添加到线性布局中。效果很好。谢谢你的帮助。
// Create an array of things to create views from.
// You will probably be getting your array from somewhere else
YourObjectHere[] things = new YourObjectHere[2];
things[0] = new YourObjectHere();
things[1] = new YourObjectHere();

// Get a reference to a LinearLayout to hold your views
LinearLayout list = (LinearLayout) findViewById(R.id.list);

// Loop through your list of objects
for(int i = 0; i < things.length; i++) {

    // Inflate an instance of your layout
    View listItem = getLayoutInflater().inflate(R.layout.child, null);

    // Set the TextViews to the appropriate things
    ((TextView) listItem.findViewById(R.id.name)).setText(things[i].getName());
    ((TextView) listItem.findViewById(R.id.type)).setText(things[i].getType());

    // Add the inflated view to the list
    item.addView(child);
}