java - TableRow 宽度不一致 - Android

标签 java android textview tablelayout tablerow

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

initial view

after switching away from the app and coming back

下面找到用于生成表格的代码

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

关于java - TableRow 宽度不一致 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57352701/

相关文章:

java - Selenium Web 驱动程序 : findElement(By. 名称 ..... 和 headless 浏览器

java - AWS Java putObject InvalidRedirectLocation

android - 在 TextView 中更改不同字符的大小,Android

java - 协变返回类型是否根据 jls 重写?

java - 如何在使用 HTMLEditorKit 构建的 HTML 编辑器中提供更改文本部分背景颜色的功能

java - JSON 解析为 ArrayList。如何让它在 onPostExecute 中重复多个 map 标记的数据?

android - 如何从新的 Lollipop 工具栏中删除膨胀的菜单/项目?

android-layout - 如何在不可见的文本字段中输入文本

java - 如何在布局中将 TextView 与背景对齐 - Android

android - 在焦点上更改可单击的 TextView 颜色并单击?