2013-12-09 57 views
1

我在ScrollView上滚动的TableLayout有问题,该代码的TableRows通过代码添加。由于某种原因,如果我使用HorizontalScrollView这将水平滚动,但不垂直(显然),而我需要垂直和水平。但是,使用带有水平和垂直滚动条的ScrollView不能。Scrollview不适用于TableLayout

我已经定义了一个ScrollView和在下面的XML一个TableLayout

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/ScrollView1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:scrollbars="horizontal|vertical" > 

<TableLayout 
    android:id="@+id/table_id" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 
</TableLayout> 
</ScrollView> 

我然后添加tablerows到tablelayout如下,

table = (TableLayout) findViewById(R.id.table_id); 
    for (int i = 0; i < 9; i++) { 
     TextView headerRow = new TextView(getApplicationContext()); 

     // set up the keyboard so it doesn't go fullscreen 
     //firstEditText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI); 

     switch (i) { 

     case 0: 
      headerRow.setText("B/Sight"); 
      break; 

     case 1: 
      headerRow.setText("Int/Sight"); 
      break; 

     case 2: 
      headerRow.setText("F/Sight"); 
      break; 

     case 3: 
      headerRow.setText("Rise"); 
      break; 

     case 4: 
      headerRow.setText("Fall"); 
      break; 

     case 5: 
      headerRow.setText("Reduced Level"); 
      break; 

     case 6: 
      headerRow.setText("Lat"); 
      break; 

     case 7: 
      headerRow.setText("Long"); 
      break; 

     case 8: 
      headerRow.setText("Remark"); 
      break; 
     } 

     setCellProperties(headerRow); 
     firstRow.addView(headerRow); 

    } 

    table.addView(firstRow); 

setCellProperties()是,

void setCellProperties(TextView txtView) { 
    txtView.setPadding(30, 15, 30, 15); 
    txtView.setTextColor(Color.BLACK); 
    txtView.setBackgroundResource(R.drawable.cell_shape); 
    txtView.setGravity(Gravity.CENTER); 
} 

然后我使用数据添加更多行与上述相同的程序。我曾尝试在其他布局(如LinerLayout)中包装此ScrollView,但我无法破解它。可能值得一提的是,这将在Landscape not Portrait中进行查看。

回答

2

感谢输入工程师,但周围的一些更下面搞乱后似乎解决它。

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:scrollbars="horizontal|vertical" > 

     <ScrollView 
      android:id="@+id/scrollview" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" > 

      <TableLayout 
       android:id="@+id/table_id" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </TableLayout> 
     </ScrollView> 
    </HorizontalScrollView> 
+1

欢迎@ Mika571和ScrollView应该是根布局。它不应该包含在Linearlayout中 –

2

尝试:

<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="220dp" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/linearLayout1" 
    android:background="#000000" > 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <TableLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_below="@+id/linearLayout1" > 
     </TableLayout> 
    </RelativeLayout> 
</ScrollView> 
+0

从你的xml应该'Scrollview'包裹在'LinearLayout'中吗? – Mika571

+0

不,在相对布局,这里工作正常 –

相关问题