我在本周早些时候选择了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工作? 谢谢你的帮助。
答案 0 :(得分:2)
我认为@ FD_的答案是正确的,但是,如果您仍然希望按照提议的方式进行,那么正确的方法是创建一个ScrollView
对象,然后通过添加TableLayout
它的addView()
方法。
ScrollView sv = new ScrollView(this);
sv.addView(yourTableLayout);
答案 1 :(得分:0)
您应该查看ListView小部件。 ListViews的设计效率更高,即使有数百行,也可以流畅地存储和工作。只有在需要时,ListView才会实例化它们的单元格,而同时实例化数百个视图。此外,ListViews已经内置了整个滚动功能。
以下是帮助您入门的教程:http://www.vogella.com/tutorials/AndroidListView/article.html