ScrollView with TableLayout programmatically Java

2019-03-03 19:47发布

问题:

I have tried everything but my ScrollView doesn't work in my activity. I have activity where after user clicks button opens a table. I can't find a way that my table scrolls bouth ways. How can I make ScrollView programmatically? My code:

    TableLayout tableLayout = new TableLayout(getApplicationContext());
    tableLayout.setVerticalScrollBarEnabled(true);
    tableLayout.setBackgroundColor(Color.WHITE);
    TableRow tableRow;
    TextView textView,...
    tableRow = new TableRow(getApplicationContext());
    textView=new TextView(getApplicationContext());
    textView.setText("Date");
    textView.setTextColor(Color.BLACK);
    textView.setTypeface(null, Typeface.BOLD);
    textView.setPadding(20, 20, 20, 20);
    tableRow.addView(textView);
    tableLayout.addView(tableRow);
    for (Integer j = 0; j < count; j++)
    {
        tableRow = new TableRow(getApplicationContext());
        textView1 = new TextView(getApplicationContext());
        ....
   tableLayout.addView(tableRow);
        c.moveToNext() ;
    }
    c.close();
    setContentView(tableLayout);
    database.close();

回答1:

You can make it this way and in ScrollView there must be only one child:

ScrollView scrollview = new ScrollView(context);
scrollview.setBackgroundColor(android.R.color.transparent);
scrollview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                                             LayoutParams.FILL_PARENT));
scrollview.addView(tableLayout);