Android 格式化新TableRow(以编程方式)
在XML中,我定义了一个带有TableRow(表的标题)的TableLayout。 每隔一行将以编程方式添加。问题是:我无法使新行与XML中的一行对齐Android 格式化新TableRow(以编程方式),android,format,row,android-tablelayout,Android,Format,Row,Android Tablelayout,在XML中,我定义了一个带有TableRow(表的标题)的TableLayout。 每隔一行将以编程方式添加。问题是:我无法使新行与XML中的一行对齐 3个TextView数组,1个TableRow数组 循环使用TextView和TableRows填充这些数组 所有参数都提供给setTableRowParams方法 为文本视图设置文本 将所有行添加到TableLayout driversNames = new TextView[DRIVERSAMOUNT]; driversDriven = ne
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]);
}
您能告诉我我的错误在哪里吗?您是否尝试过在子视图中将android:layout\u width属性设置为0dp(当然,将android:layout\u权重设置为1)
确保所有的孩子都从相同的宽度开始(android:layout_width=“0dp”)谢谢Fahim,但我不确定你的意思。我应该将哪个元素宽度设置为0dp?我已经用XML部分更新了我的问题。
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>