有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java表行宽不一致Android

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

initial view

after switching away from the app and coming back

下面是生成表格的代码

XML

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

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

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

        </HorizontalScrollView>

爪哇

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++;
            }

我做错了什么?我如何保持相同的列宽,而不管用户是离开并返回应用程序,还是在应用程序上停留很长时间


共 (0) 个答案