不一致的 TableRow 宽度 - Android

Inconsistent TableRow width - Android

我有一个动态填充的 TableLayout。进入页面后,它按预期显示,但当我离开应用程序或来电时,布局扭曲,导致列宽不一致,如下图所示。

在下面找到用于生成table

的代码

XML

<HorizontalScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scrollbars="vertical|horizontal"
            android:background="@color/colorPrimaryDark">

            <TableLayout
                android:id="@+id/predictionTableView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="1dp"
                android:stretchColumns="*"
                android:background="@color/colorPrimaryDark">

                <TableRow>
                    <TextView
                        android:text="RESULT"
                        android:padding="1dp"
                        android:textSize="12sp"
                        android:textColor="@color/white"
                        android:background="#000"
                        android:layout_marginRight="1dp"/>
                    <TextView
                        android:text="TIME"
                        android:padding="1dp"
                        android:textSize="12sp"
                        android:textColor="@color/white"
                        android:layout_marginRight="1dp"
                        android:background="#000"/>
                    <TextView
                        android:text="LEAGUE"
                        android:padding="1dp"
                        android:textSize="12sp"
                        android:textColor="@color/white"
                        android:layout_marginRight="1dp"
                        android:background="#000"/>
                    <TextView
                        android:text="TEAM"
                        android:padding="1dp"
                        android:textSize="12sp"
                        android:textColor="@color/white"
                        android:layout_marginRight="1dp"
                        android:background="#000"/>
                    <TextView
                        android:text="PREDICTION"
                        android:padding="1dp"
                        android:textSize="12sp"
                        android:textColor="@color/white"
                        android:layout_marginRight="1dp"
                        android:background="#000"/>
                    <TextView
                        android:text="SCORE"
                        android:padding="1dp"
                        android:textSize="12sp"
                        android:textColor="@color/white"
                        android:background="#000"/>
                </TableRow>
            </TableLayout>

        </HorizontalScrollView>

Java

int i = 1;
            for (Prediction prediction : predictionCategory.getPredictions()) {
                //int backgroundColor = (i%2 == 0 ? fragmentActivity.getResources().getColor(R.color.colorPrimaryDark) : fragmentActivity.getResources().getColor(R.color.colorAccent));
                Drawable backgroundDrawable = fragmentActivity.getResources().getDrawable(R.drawable.table_cell_even); //(i%2 == 0 ? fragmentActivity.getResources().getDrawable(R.drawable.table_cell_even) : fragmentActivity.getResources().getDrawable(R.drawable.table_cell_odd));
                int textColor = fragmentActivity.getResources().getColor(R.color.white);

                TableRow tableRow = new TableRow(fragmentActivity);
                tableRow.setBackground(backgroundDrawable);

                int colWidth = predictionTableView.getChildAt(0).getWidth();

                TextView timeTextView = new TextView(fragmentActivity);
                TextView leagueTextView = new TextView(fragmentActivity);
                TextView teamTextView = new TextView(fragmentActivity);
                TextView predictionTextView = new TextView(fragmentActivity);
                TextView scoreTextView = new TextView(fragmentActivity);
                TextView resultTextView = new TextView(fragmentActivity);

                timeTextView.setTypeface(Typeface.DEFAULT);
                timeTextView.setText(prediction.getTime());
                timeTextView.setTextSize(13);
                timeTextView.setPadding(3, 3, 3, 3);
                timeTextView.setBackground(backgroundDrawable);
                timeTextView.setTextColor(textColor);

                leagueTextView.setTypeface(Typeface.DEFAULT);
                leagueTextView.setText(prediction.getLeagueName());
                leagueTextView.setTextSize(13);
                leagueTextView.setPadding(3, 3, 3, 3);
                leagueTextView.setBackground(backgroundDrawable);
                leagueTextView.setTextColor(textColor);

                teamTextView.setTypeface(Typeface.DEFAULT);
                teamTextView.setText(prediction.getLocalTeamName() + " - " + prediction.getVisitorTeamName());
                teamTextView.setTextSize(13);
                teamTextView.setPadding(3, 3, 3, 3);
                teamTextView.setBackground(backgroundDrawable);
                teamTextView.setTextColor(textColor);

                predictionTextView.setTypeface(Typeface.DEFAULT);
                predictionTextView.setText(prediction.getPrediction());
                predictionTextView.setTextSize(13);
                predictionTextView.setPadding(3, 3, 3, 3);
                predictionTextView.setBackground(backgroundDrawable);
                predictionTextView.setTextColor(textColor);

                scoreTextView.setTypeface(Typeface.DEFAULT);
                scoreTextView.setText(prediction.getScores());
                scoreTextView.setTextSize(13);
                scoreTextView.setPadding(3, 3, 3, 3);
                scoreTextView.setBackground(backgroundDrawable);
                scoreTextView.setTextColor(textColor);

                resultTextView.setTypeface(Typeface.DEFAULT);
                resultTextView.setText(prediction.getResult());
                resultTextView.setTextSize(13);
                resultTextView.setPadding(3, 3, 3, 3);
                resultTextView.setBackground(backgroundDrawable);
                resultTextView.setTextColor(textColor);
                if (!prediction.getResult().matches("")) {
                    if (prediction.getResult().toUpperCase().matches("WIN")) {
                        resultTextView.setBackgroundColor(fragmentActivity.getResources().getColor(R.color.colorSuccess));
                    } else {
                        resultTextView.setBackgroundColor(fragmentActivity.getResources().getColor(R.color.colorDanger));
                    }
                } /*else {
                    resultTextView.setText("LOST");
                    resultTextView.setBackgroundColor(fragmentActivity.getResources().getColor(R.color.colorDanger));
                }*/

                tableRow.addView(resultTextView);
                tableRow.addView(timeTextView);
                tableRow.addView(leagueTextView);
                tableRow.addView(teamTextView);
                tableRow.addView(predictionTextView);
                tableRow.addView(scoreTextView);

                TableLayout.LayoutParams trParamsSep = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
                tableRow.setLayoutParams(trParamsSep);

                predictionTableView.addView(tableRow);
                i++;
            }

我做错了什么?无论用户是离开并返回应用程序还是长时间停留在应用程序上,我如何保持相等的列宽。

重新加载数据 onResume() 对我来说效果很好。谢谢@SumitShukla