Android 无法从片段内更新TableLayout
我正在开发一个应用程序,它可以让用户进行一次测验,并记录所有测验分数,以便以后需要时参考。我正在构建一个片段,允许用户在TableLayout中查看此记录。我需要动态添加行,所以我正在尝试创建一种在片段的onCreateView方法期间添加行的方法。但是,尽管这些行在调试器中显示为表的子行,但它们从未在应用程序中显示。我试着先拆下碎片,然后再重新连接,但这根本没用。我非常感激能想出这个办法 这是片段:Android 无法从片段内更新TableLayout,android,android-studio,android-fragments,android-tablelayout,Android,Android Studio,Android Fragments,Android Tablelayout,我正在开发一个应用程序,它可以让用户进行一次测验,并记录所有测验分数,以便以后需要时参考。我正在构建一个片段,允许用户在TableLayout中查看此记录。我需要动态添加行,所以我正在尝试创建一种在片段的onCreateView方法期间添加行的方法。但是,尽管这些行在调试器中显示为表的子行,但它们从未在应用程序中显示。我试着先拆下碎片,然后再重新连接,但这根本没用。我非常感激能想出这个办法 这是片段: public class GalleryFragment extends Fragment {
public class GalleryFragment extends Fragment {
private GalleryViewModel galleryViewModel;
public void addEntryToTable(TableLayout table, String user, String date, String score) {
QuizHistoryTableRow row = new QuizHistoryTableRow(getContext(), user, date, score);
table.addView(row);
}
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_gallery, container, false);
TableLayout table = root.findViewById(R.id.quiz_history_table);
addEntryToTable(table, "user", "date", "score");
return root;
}
}
public class GalleryFragment extends Fragment {
private GalleryViewModel galleryViewModel;
public TableRow getNewEntry(Context context, String user, String date, String score) {
QuizHistoryTableRow row = new QuizHistoryTableRow(context, user, date, score);
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
return row;
}
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_gallery, container, false);
TableLayout table = root.findViewById(R.id.quiz_history_table);
TableRow row = getNewEntry(getContext(), "user", "date", "score");
table.addView(row, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
return root;
}
}
这是自定义表行:
public class QuizHistoryTableRow extends TableRow {
public QuizHistoryTableRow(Context context, String user, String date, String score){
super(context);
TextView userView = new TextView(context);
userView.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
userView.setText(user);
userView.setTextColor(0);
userView.setTextSize(22);
TextView dateView = new TextView(context);
dateView.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
dateView.setText(date);
dateView.setTextColor(0);
dateView.setTextSize(22);
TextView scoreView = new TextView(context);
scoreView.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
scoreView.setText(score);
scoreView.setTextColor(0);
scoreView.setTextSize(22);
addView(userView);
addView(dateView);
addView(scoreView);
}
}
public class QuizHistoryTableRow extends TableRow {
public QuizHistoryTableRow(Context context, String user, String date, String score){
super(context);
TextView userView = new TextView(context);
userView.setText(user);
userView.setTextColor(Color.BLACK);
userView.setTextSize(22);
userView.setLayoutParams(new LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
TextView dateView = new TextView(context);
dateView.setText(date);
dateView.setTextColor(Color.BLACK);
dateView.setTextSize(22);
dateView.setLayoutParams(new LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
TextView scoreView = new TextView(context);
scoreView.setText(score);
scoreView.setTextColor(Color.BLACK);
scoreView.setTextSize(22);
scoreView.setLayoutParams(new LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
addView(userView);
addView(dateView);
addView(scoreView);
}
}
修复了它,这是与
TableLayout.LayoutParams
vsTableRow.LayoutParams
有关的问题。谢谢迈克M.的回答
修改片段:
public class GalleryFragment extends Fragment {
private GalleryViewModel galleryViewModel;
public void addEntryToTable(TableLayout table, String user, String date, String score) {
QuizHistoryTableRow row = new QuizHistoryTableRow(getContext(), user, date, score);
table.addView(row);
}
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_gallery, container, false);
TableLayout table = root.findViewById(R.id.quiz_history_table);
addEntryToTable(table, "user", "date", "score");
return root;
}
}
public class GalleryFragment extends Fragment {
private GalleryViewModel galleryViewModel;
public TableRow getNewEntry(Context context, String user, String date, String score) {
QuizHistoryTableRow row = new QuizHistoryTableRow(context, user, date, score);
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
return row;
}
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_gallery, container, false);
TableLayout table = root.findViewById(R.id.quiz_history_table);
TableRow row = getNewEntry(getContext(), "user", "date", "score");
table.addView(row, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
return root;
}
}
和修改后的表行:
public class QuizHistoryTableRow extends TableRow {
public QuizHistoryTableRow(Context context, String user, String date, String score){
super(context);
TextView userView = new TextView(context);
userView.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
userView.setText(user);
userView.setTextColor(0);
userView.setTextSize(22);
TextView dateView = new TextView(context);
dateView.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
dateView.setText(date);
dateView.setTextColor(0);
dateView.setTextSize(22);
TextView scoreView = new TextView(context);
scoreView.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
scoreView.setText(score);
scoreView.setTextColor(0);
scoreView.setTextSize(22);
addView(userView);
addView(dateView);
addView(scoreView);
}
}
public class QuizHistoryTableRow extends TableRow {
public QuizHistoryTableRow(Context context, String user, String date, String score){
super(context);
TextView userView = new TextView(context);
userView.setText(user);
userView.setTextColor(Color.BLACK);
userView.setTextSize(22);
userView.setLayoutParams(new LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
TextView dateView = new TextView(context);
dateView.setText(date);
dateView.setTextColor(Color.BLACK);
dateView.setTextSize(22);
dateView.setLayoutParams(new LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
TextView scoreView = new TextView(context);
scoreView.setText(score);
scoreView.setTextColor(Color.BLACK);
scoreView.setTextSize(22);
scoreView.setLayoutParams(new LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
addView(userView);
addView(dateView);
addView(scoreView);
}
}
userView.setTextColor(0)
–零是透明的。如果你想让它是黑色的,那么它应该是0xff000000
。不过,最好使用@MikeM。谢谢你的回复,我把它改成了黑色,但由于某种原因它还是没有出现。有什么想法吗?哦,是的,这是布局参数
;至少,还有一件事。视图
的布局参数
必须是其父级的类型,因为它是父级了解如何布局该子级的信息。在这种情况下,您的文本视图
位于表格行
,因此它们需要表格行.布局参数
,而不是表格布局.布局参数
。有时,某些类型的LayoutParams
可以成功转换为另一种类型,但TableLayout
对此非常挑剔。由于您位于TableRow
类中,因此您可以从这些构造函数调用的开头删除TableLayout.
。此外,尽管这不会导致当前问题,但我还要提到,无论何时使用权重,与方向相对应的维度的值应为0
。也就是说,因为它们是水平的,所以宽度应该是0
。总之:newlayoutparams(0,LayoutParams.WRAP_CONTENT,1f)
@MikeM。谢谢,我会的。