2011-03-22 64 views
4

我有一個tableview,我想滾動,因爲數據沒有顯示完整。如何在android中滾動tableview

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:stretchColumns="0,1,2,3,4" 
      android:id="@+id/maintable" > 
</TableLayout> 

這是我tablelayout,如果我在<ScrollView> </Scrollview>應用crashs包裹它,如果我打開活動。怎麼做?

+0

android listview直接滾動時,列表大於大小,所以你有沒有嘗試把許多行在表視圖? – Siten 2011-03-22 13:09:15

+0

不,我以爲我必須使用tablelayout這個?因爲我只顯示一張桌子? ^^ – Mark 2011-03-22 13:18:38

+0

你可以把你的完整的XML文件? – 2011-03-22 13:22:47

回答

11

你應該真的把你試過的代碼弄糟了你的應用程序和崩潰的原因。原始帖子中沒有有價值的信息。

Ehhh ...你嘗試過這麼簡單嗎?我使用幾個地方的示例.xml:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TableLayout android:id="@+id/score_table" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <TableRow android:id="@+id/header"/>      
    </TableLayout> 
</ScrollView> 

對我來說絕對沒問題。顯然,如果您沒有使用TableRow,則無需包含TableRow。

+0

這將滾動鑽孔表我不想滾動標題然後呢? – CoronaPintu 2014-02-13 04:54:40

2

創建行動態 在這裏,我把一個小例子,

main.xml中

<Button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add row"></Button> 

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

    <TableLayout android:id="@+id/TableLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="0"> 

    <TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content" android:layout_height="wrap_content"> 
     <TextView android:id="@+id/TextView01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="textfield 1-1"></TextView> 

     <CheckBox android:id="@+id/CheckBox01" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> 
    </TableRow> 

    </TableLayout> 
</ScrollView> 

活動是

public class tablelayout extends Activity implements OnClickListener { 
    /** Called when the activity is first created. */ 

    //initialize a button and a counter 
    Button btn; 
    int counter = 0; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // setup the layout 
     setContentView(R.layout.main); 

     // add a click-listener on the button 
     btn = (Button) findViewById(R.id.Button01); 
     btn.setOnClickListener(this);   

    } 

    // run when the button is clicked 
    public void onClick(View view) { 

     // get a reference for the TableLayout 
     TableLayout table = (TableLayout) findViewById(R.id.TableLayout01); 

     // create a new TableRow 
     TableRow row = new TableRow(this); 

     // count the counter up by one 
     counter++; 

     // create a new TextView 
     TextView t = new TextView(this); 
     // set the text to "text xx" 
     t.setText("text " + counter); 

     // create a CheckBox 
     CheckBox c = new CheckBox(this); 

     // add the TextView and the CheckBox to the new TableRow 
     row.addView(t); 
     row.addView(c); 

     // add the TableRow to the TableLayout 
     table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

    } 
} 

您可以創建錶行,當你的代碼需要時。我認爲它是在按鈕點擊。

希望這會有所幫助。

而且這裏還有一個example

+0

不,這不是問題,生病讓我的表像我想要的。我用這個:[link](http://www.warriorpoint.com/blog/2009/07/01/android-creating-tablerow-rows-inside-a-tablelayout-programatically/)。但它不顯示屏幕上的所有內容,所以我想滾動,但那不起作用 – Mark 2011-03-22 13:47:55

+0

你能解釋哪部分不顯示,是水平還是垂直? – 2011-03-22 16:04:38

+1

還有一件事,如果你想滾動,你的表必須在滾動視圖。 – 2011-03-22 16:10:36