格式化新的TableRow(以编程方式)

时间:2015-02-18 23:00:44

标签: android format row android-tablelayout

XML中的

我用TableRow(表格的标题)定义了一个TableLayout。 每个其他行将以编程方式添加。问题是:我无法将新行与XML

排成一行
  1. 3个TextView数组,1个TableRow数组
  2. 使用TextViews和TableRows
  3. 循环填充这些数组
  4. 所有都被赋予setTableRowParams方法
  5. 设置TextViews的文本
  6. 将所有行添加到TableLayout

    driversNames = new TextView[DRIVERSAMOUNT];
    driversDriven = new TextView[DRIVERSAMOUNT];
    driversRemaining = new TextView[DRIVERSAMOUNT];
    driversRows = new TableRow[DRIVERSAMOUNT];
    
    for (int i = 0; i < DRIVERSAMOUNT; i++) {
    
        driversNames[i] = new TextView(this);
        driversDriven[i] = new TextView(this);
        driversRemaining[i] = new TextView(this);
    
        driversRows[i] = new TableRow(this);
        setTableRowParams(driversRows[i]);
    
        driversNames[i].setText(driversArray[i].getName());
        driversDriven[i].setText("" + driversArray[i].getTimeDriven());
        driversRemaining[i].setText("" + driversArray[i].getTimeRemaining());
    
    
    
        driversRows[i].addView(driversNames[i]);
        driversRows[i].addView(driversDriven[i]);
        driversRows[i].addView(driversRemaining[i]);
    
        tableLayout.addView(driversRows[i]);
    
    }
    

    setTableRowParams()方法

    TableLayout.LayoutParams tableRowParams =
            new TableLayout.LayoutParams
                    (TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
    tableRowParams.setMargins(10, 10, 10, 10);
    
    tr.setLayoutParams(tableRowParams);
    
        <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/card_driver"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="20dp"
            android:elevation="10dp"
            card_view:cardCornerRadius="2dp">
    
            <TableLayout
                android:id="@+id/tableDrivers"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <TableRow
                    android:id="@+id/driversRow1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="10dp">
    
                    <TextView
                        android:id="@+id/tVDriversTitle"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_span="3"
                        android:layout_weight="1"
                        android:text="@string/card_drivers"
                        android:textAppearance="?android:attr/textAppearanceLarge" />
    
                </TableRow>
    
                <TableRow
                    android:id="@+id/driversRow2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="10dp">
    
                    <TextView
                        android:id="@+id/tVDriversName"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="@string/drivers"
                        android:textAppearance="?android:attr/textAppearanceMedium" />
    
                    <TextView
                        android:id="@+id/tVDriversDriven"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="@string/timeDriven"
                        android:textAppearance="?android:attr/textAppearanceMedium" />
    
                    <TextView
                        android:id="@+id/tVDriversRemaining"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="@string/timeLeft"
                        android:textAppearance="?android:attr/textAppearanceMedium" />
    
                </TableRow>
    
                <!-- THE VALUES ARE ADDED PROGRAMATICALLY -->
    
            </TableLayout>
    
        </android.support.v7.widget.CardView>
    
  7. 这是结果。正如您所看到的那样,Driver 1-4的行不会与标题对齐。对不起外语; - )enter image description here

    你能告诉我我的错误在哪里吗?

1 个答案:

答案 0 :(得分:0)

您是否尝试将android:layout_width属性设置为0dp(并且当然将android:layout_weight保持为1)在子视图上?

确保所有孩子都从相同的宽度开始(两者都是android:layout_width =“0dp”)