水平和垂直滚动

时间:2014-03-21 04:53:24

标签: android android-layout

我正在构建一个表来显示数据库中的项目。我目前正在尝试启用滚动功能,以便所有列和行都可见,但它不起作用。该表未绑定到xml文件。这是生成表的代码部分。

    public void showdata(View view)
{
    Cursor c=db.rawQuery("SELECT * from Student", null);
     int count= c.getCount();
    c.moveToFirst();
    TableLayout tableLayout = new TableLayout(getApplicationContext());
    tableLayout.setVerticalScrollBarEnabled(true);
    tableLayout.setHorizontalScrollBarEnabled(true);
   TableRow tableRow;
   TextView textView,textView1,textView2,textView3,textView4,textView5,textView6,textView7;
   tableRow = new TableRow(getApplicationContext());
   textView=new TextView(getApplicationContext());
   textView.setText("Module Code");
   textView.setTextColor(Color.RED);
    textView.setTypeface(null, Typeface.BOLD);
     textView.setPadding(20, 20, 20, 20);
    tableRow.addView(textView);
    textView4=new TextView(getApplicationContext());
    textView4.setText("Assignment Name");
    textView4.setTextColor(Color.RED);
    textView4.setTypeface(null, Typeface.BOLD);
     textView4.setPadding(20, 20, 20, 20);
    tableRow.addView(textView4);
    textView5=new TextView(getApplicationContext());
    textView5.setText("Marks Proportions");
    textView5.setTextColor(Color.RED);
    textView5.setTypeface(null, Typeface.BOLD);
    textView5.setPadding(20, 20, 20, 20);
    tableRow.addView(textView5);
    textView6=new TextView(getApplicationContext());
    textView6.setText("Due Date");
    textView6.setTextColor(Color.RED);
    textView6.setTypeface(null, Typeface.BOLD);
    textView6.setPadding(20, 20, 20, 20);
    tableRow.addView(textView6);
   tableLayout.addView(tableRow);
     for (Integer j = 0; j < count; j++)
     {
         tableRow = new TableRow(getApplicationContext());
         textView1 = new TextView(getApplicationContext());
         textView1.setText(c.getString(c.getColumnIndex("moduleCode")));
         textView2 = new TextView(getApplicationContext());
         textView2.setText(c.getString(c.getColumnIndex("assignmentName")));
         textView3 = new TextView(getApplicationContext());
         textView3.setText(c.getString(c.getColumnIndex("marksProportion")));
         textView7 = new TextView(getApplicationContext());
         textView7.setText(c.getString(c.getColumnIndex("dueDate")));
         textView1.setPadding(20, 20, 20, 20);
         textView2.setPadding(20, 20, 20, 20);
         textView3.setPadding(20, 20, 20, 20);
         textView7.setPadding(20, 20, 20, 20);
         tableRow.addView(textView1);
         tableRow.addView(textView2);
         tableRow.addView(textView3);
         tableRow.addView(textView7);
         tableLayout.addView(tableRow);
         c.moveToNext() ;
     }
     setContentView(tableLayout);
db.close();
}

非常感谢任何帮助或建议。

1 个答案:

答案 0 :(得分:0)

ListView会更有效率。如果您只想显示整个TableLayout,然后将其添加到ScrollView ...然后使用setContentView(scrollView)。