无法以编程方式创建TableLayout

时间:2014-08-09 20:50:29

标签: java android xml layout tablelayout

我已经在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>

根据Nathan Walter的建议,这是我尝试使用的新代码。但是,它会设置第一个设置文本,但其余的只是默认值。虽然布局看起来正确。如何在调用方法时为每个单独的视图设置文本字段。

public void Display(String name, String ctroom) {
    ViewGroup parent = (ViewGroup) findViewById(R.id.container);

    view = LayoutInflater.from(this).inflate(R.layout.tablebutton, null);
    parent.addView(view);
    TextView jn = (TextView) findViewById(R.id.JudgenameCourts);
    jn.setText(name);

    TextView ct = (TextView) findViewById(R.id.Courtroomnew);
    ct.setText(ctroom);

    view = LayoutInflater.from(this).inflate(R.layout.tablebutton, null);
    parent.addView(view);

    jn.setText("Name1");

    ct.setText("Courtroom1");

}

1 个答案:

答案 0 :(得分:0)

我认为你想要的代码看起来像这样。如果有意义,请告诉我。

// 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);
}
相关问题