2013-03-13 109 views
0

我已经在相对布局中动态创建了动态表格布局。我正在动态添加文本视图。我有9个字段可以显示。我怎样才能把水平和垂直滚动条呢?以相对布局将滚动条添加到动态表格视图

代码:

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_view); 
     // Show the Up button in the action bar. 
     setupActionBar(); 

     TableLayout tableLayout = new TableLayout(getApplicationContext()); 
     TableRow tableRow; 
      TextView textView; 


      ............................ 
      ............................ 
      ............................ 
      for (int i = 0; i < rows; i++) 
      {        //4 rows 
      tableRow = new TableRow(getApplicationContext()); 

      for (int j = 0; j < columns; j++) 
      {       //3 columns 
       textView = new TextView(getApplicationContext()); 

       textView.setText(temp[k++]); 
       textView.setPadding(15, 15, 15, 15); 
       tableRow.addView(textView); 

      } 

      tableLayout.addView(tableRow); 
     } 

      setContentView(scrollView); 
     setContentView(tableLayout); 

回答

0

让您的顶层布局ScrollView

scrollView.addView(tableLayout); 
setContentView(scrollView); 
+0

我是新来的Android。其实我已经试过了。它不起作用。你能确切地告诉我在哪里我应该把这个在我的代码? – user2151150 2013-03-13 12:40:19

1

像这样创建

<RelativeLayout xmlns:ads="http://schemas.android.com/apk/res/com.smartcloud.podcast_he" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/bg_iphone" > 
     <ScrollView 
      android:id="@+id/ScrollView03" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/headingtitle2" > 

     </ScrollView> 
</RelativeLayout> 

然后在你的代码文件layout.xml

setContentView(R.layout.activity_view); 
     // Show the Up button in the action bar. 
setupActionBar(); 

ScrollView scrollview = (ScrollView) findViewById(R.id.ScrollView03); 

TableLayout tableLayout = new TableLayout(getApplicationContext()); 
    TableRow tableRow; 
     TextView textView; 


     ............................ 
     ............................ 
     ............................ 
     for (int i = 0; i < rows; i++) 
     {        //4 rows 
     tableRow = new TableRow(getApplicationContext()); 

     for (int j = 0; j < columns; j++) 
     {       //3 columns 
      textView = new TextView(getApplicationContext()); 

      textView.setText(temp[k++]); 
      textView.setPadding(15, 15, 15, 15); 
      tableRow.addView(textView); 

     } 

     tableLayout.addView(tableRow); 
    } 
scrollview.addView(tableLayout); 

删除此2号线

setContentView(scrollView); 
    setContentView(tableLayout); 
+0

太好了。它适用于垂直滚动条。我怎样才能把水平的一个? – user2151150 2013-03-13 12:55:43