【问题标题】:Horizontal and Vertical scrolling水平和垂直滚动
【发布时间】:2014-03-21 04:53:24
【问题描述】:

我正在构建一个表格来显示数据库中的项目。我目前正在尝试启用滚动,以便所有列和行都可见,但它不起作用。该表未绑定到 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();
}

任何帮助或建议将不胜感激。

【问题讨论】:

标签: android android-layout


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2014-01-09
    • 2020-08-17
    • 2015-05-31
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-12
    相关资源
    最近更新 更多