java - 以编程方式 ScrollView

标签 java android scrollview

我在本周早些时候学习了 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/

相关文章:

listview - Scrollview 中的 Xamarin Forms Listview 不滚动

ScrollView 中的 Android TextView 无法正常工作 - 强制关闭

java - 检查素数的类不能正常工作

java - 如果将静态变量分配给局部变量怎么办?

android - 在游戏中,如果我从小部件运行,图像会在 Android 设备上消失,但在我第一次安装 apk 时不会

java - 将 InputStream 转换为 BufferedReader

ios - Xcode 在没有内容 View 的情况下直接嵌入 ScrollView

java - 回溯算法最大化两个房间中的盒子数量

Java 进程在 Linux 后台运行时停止

android - 在 Android 中获取屏幕尺寸