0

我正在尝试构建一个动态填充表。我知道了,但我的问题是添加的单元格与标题栏的宽度不匹配。

我的桌子

我怎样才能做到这一点?

布局.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:orientation="horizontal" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/txtError"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:textSize="25sp" />

        <TableLayout
            android:id="@+id/timelineTable"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/txtError"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="5dp"
            android:background="#000000" >

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="0.5dp"
                android:background="@android:color/black"
                android:textAlignment="center" >

                <TextView
                    android:id="@+id/txtHeaderDate"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight=".20"
                    android:background="@drawable/cell_shape"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="@string/TimelineHeaderDate"
                    android:textSize="20sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/txtHeaderType"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight=".20"
                    android:background="@drawable/cell_shape"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="@string/TimelineHeaderType"
                    android:textSize="20sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/txtHeaderValue"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight=".20"
                    android:background="@drawable/cell_shape"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="@string/TimelineHeaderValue"
                    android:textSize="20sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/txtHeaderUnit"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight=".15"
                    android:background="@drawable/cell_shape"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="@string/TimelineHeaderUnit"
                    android:textSize="20sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/txtHeaderComment"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight=".25"
                    android:background="@drawable/cell_shape"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="@string/TimelineHeaderComment"
                    android:textSize="20sp"
                    android:textStyle="bold" />
            </TableRow>
        </TableLayout>
    </RelativeLayout>

</ScrollView>

动态添加:

private void printTimeline() {
  Log.d(CLASSTAG, "printTimeline");

  TableLayout timelineTable = (TableLayout) ParentActivity.findViewById(R.id.timelineTable);

  Log.d(CLASSTAG, "we have: " + this.timelineEvents.size() + " events to add");

  for (PlantEvent event : this.timelineEvents) {

    TableRow tr = new TableRow(ParentActivity);
    LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    tr.setLayoutParams(lp);


    for (int j = 1; j <= 5; j++) {

      String text = "";
      switch (j) {
      case 1:
        text = TimeHelper.convertTime(event.getDate().getTime());
        break;
      case 2:
        text = event.getType();
        break;
      case 3:
        text = (event.getDetails().get("Value") != null) ? event
            .getDetails().get("Value") : "";
        break;
      case 4:
        text = (event.getDetails().get("Unit") != null) ? event
            .getDetails().get("Unit") : "";
        break;
      case 5:
        text = (event.getDetails().get("comment") != null) ? event
            .getDetails().get("comment") : "";
        break;

      }

      TextView txtField = new TextView(ParentActivity);
      txtField.setText(text);

      txtField.setBackgroundColor(Color.GRAY);
      txtField.setGravity(Gravity.CENTER);
      tr.addView(txtField);

      txtField.setLayoutParams(lp);

    }

    timelineTable.addView(tr);
  }

}    
4

1 回答 1

1

尝试这个:

private void printTimeline() {
    Log.d(CLASSTAG, "printTimeline");

    TableLayout timelineTable = (TableLayout) ParentActivity.findViewById(R.id.timelineTable);

    Log.d(CLASSTAG, "we have: " + this.timelineEvents.size() + " events to add");

    for (PlantEvent event : this.timelineEvents) {

      TableRow tr = new TableRow(ParentActivity);
      LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
      tr.setLayoutParams(lp);

      float []weights = {0.2f, 0.2f, 0.2f, 0.15f, 0.25f};
      Display display = getWindowManager().getDefaultDisplay(); 
      int width = display.getWidth();  // deprecated

      for (int j = 1; j <= 5; j++) {

        String text = "";
        switch (j) {
        case 1:
          text = TimeHelper.convertTime(event.getDate().getTime());
          break;
        case 2:
          text = event.getType();
          break;
        case 3:
          text = (event.getDetails().get("Value") != null) ? event
              .getDetails().get("Value") : "";
          break;
        case 4:
          text = (event.getDetails().get("Unit") != null) ? event
              .getDetails().get("Unit") : "";
          break;
        case 5:
          text = (event.getDetails().get("comment") != null) ? event
              .getDetails().get("comment") : "";
          break;

        }

        TextView txtField = new TextView(ParentActivity);
        txtField.setText(text);

        txtField.setBackgroundColor(Color.GRAY);
        txtField.setGravity(Gravity.CENTER);


        TableRow.LayoutParams tlp = new TableRow.LayoutParams((int)(weights[j-1] * width), TableRow.LayoutParams.WRAP_CONTENT);
        txtField.setLayoutParams(tlp);
        tr.addView(txtField);

      }

      timelineTable.addView(tr);
    }

  }
于 2013-09-14T11:47:10.533 回答