我在本周早些时候学习了 Java,目前正处于创建应用程序的最后一步。 我正在尝试在以编程方式创建的表中打印数据。问题是,我似乎无法弄清楚如何向该表添加 ScrollView 。表格继续显示大量的行,所以我需要 ScrollView 才能正确查看它。我尝试过的一切都会使我的应用程序崩溃。以下是我的建表函数。
private void createTable ()
{
DecimalFormat format = new DecimalFormat ("##.00");
LinearLayout.LayoutParams linearContainerParams =
new LinearLayout.LayoutParams (
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0.0f);
LinearLayout.LayoutParams linearWidgetParams = new LinearLayout.LayoutParams (
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT,
1.0f);
TableLayout.LayoutParams tableContainerParams =
new TableLayout.LayoutParams (
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0.0f);
TableLayout.LayoutParams tableWidgetParams =
new TableLayout.LayoutParams (
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT,
1.0f);
TableRow.LayoutParams rowContainerParams =
new TableRow.LayoutParams (
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0.0f);
TableRow.LayoutParams rowWidgetParams =
new TableRow.LayoutParams (
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT,
1.0f);
mRoot = new LinearLayout (this);
mRoot.setOrientation (LinearLayout.VERTICAL);
mRoot.setBackgroundColor (Color.LTGRAY);
mRoot.setLayoutParams (linearContainerParams);
mTableLayout = new TableLayout (this);
mTableLayout.setOrientation (TableLayout.VERTICAL);
mTableLayout.setBackgroundColor (Color.BLUE);
mTableLayout.setLayoutParams (tableContainerParams);
mRoot.addView (mTableLayout);
mTableRow = new TableRow (this);
mTableRow.setOrientation (TableLayout.VERTICAL);
mTableRow.setBackgroundColor (Color.CYAN);
mTableRow.setLayoutParams (rowContainerParams);
mTableLayout.addView (mTableRow);
mTextView = new TextView (this);
mTextView.setText ("Total");
mTextView.setTextColor (Color.RED);
mTextView.setGravity (Gravity.RIGHT);
mTextView.setLayoutParams (rowWidgetParams);
mTableRow.addView (mTextView);
mTextView = new TextView (this);
mTextView.setText ("Month");
mTextView.setTextColor (Color.RED);
mTextView.setGravity (Gravity.RIGHT);
mTextView.setLayoutParams (rowWidgetParams);
mTableRow.addView (mTextView);
int i = 0;
for (i = 0; i < mTotalPaymentCount; ++i)
{
TextView text = new TextView (this);
text.setText ("" + (i + 1));
row.addView (text);
text.setTextColor (Color.RED);
text.setGravity (Gravity.RIGHT);
text.setLayoutParams (rowWidgetParams);
mTableLayout.addView (row);
}
setContentView (mRoot);
}
有什么方法可以让 ScrollView 正常工作吗? 感谢您的帮助。
最佳答案
我认为@FD_ 的回答是正确的,但是,如果您仍然想按照您建议的方式进行操作,正确的方法是创建一个 ScrollView
对象,然后添加 TableLayout
通过其 addView()
方法。
ScrollView sv = new ScrollView(this);
sv.addView(yourTableLayout);
关于java - 以编程方式 ScrollView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21074763/