2014-07-22 134 views
0

在我的項目中,我設法以編輯文本和文本視圖的行和列的形式顯示數據庫。但是隨着行數和列數的增加,無法查看增加的數據,他們不適合在屏幕上。 所以我想我需要一個水平滾動視圖和垂直滾動視圖來查看整個行和列。 以下是我的代碼,請幫助我如何實現這一點。 請你幫我在此先感謝動態創建水平和垂直滾動視圖Android

XML代碼

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical"> 



<TableLayout 
      android:layout_width="wrap_content" 
      android:stretchColumns="0,1,2,3,4,5" 
      android:id="@+id/maintable" 
      android:layout_height="wrap_content" 
      android:layout_weight="1.0"> 

</TableLayout> 
</LinearLayout> 

下面是我想展示我的行和列的網頁代碼

public class ViewActivity extends Activity { 
EditText col1; 
EditText col2; 
TextView Id; 
EditText col4; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_view); 
    ScrollView sv = new ScrollView(this); 
    HorizontalScrollView hs =new HorizontalScrollView(this); 

    TableLayout tl = (TableLayout) findViewById(R.id.maintable); 

    //CREATING A LIST OF HEADINGS 

TableRow tr1 = new TableRow(this); 
    tr1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

    Id = new TextView(this); 
    Id.setText("COL1"); 
    tr1.addView(Id); 

    Id = new TextView(this); 
    Id.setText("COL2"); 
    tr1.addView(Id); 

    Id = new TextView(this); 
    Id.setText("COL3"); 
    tr1.addView(Id); 

    Id = new TextView(this); 
    Id.setText("Rssi"); 
    tr1.addView(COL4); 

    Id = new TextView(this); 
    Id.setText("Delete"); 
    tr1.addView(COL5); 

    Id = new TextView(this); 
    Id.setText("Update"); 
    tr1.addView(COL6); 

    hs.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
      LayoutParams.FILL_PARENT)); 



    tl.addView(tr1,new TableLayout.LayoutParams(
      LayoutParams.FILL_PARENT, 
      LayoutParams.WRAP_CONTENT)); 

    // CREATING ROWS AND COLUMNS DYNAMICALLY 


    final DBclass entry = new DBclass(ViewActivity.this); 
    entry.open(); 
    Cursor c = entry.getAllRecords(); 

    int count =0; 

    if(c.moveToFirst()){ 
     do{ 
      TableRow tr = new TableRow(this); 
      tr.setId(count); 
      tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

      /* CODE FOR 
       COLUMNS DISPLAYED IN THE FORM OF 
       EDITTEXTS AND TEXTVIEWS 
      */ 

      tl.addView(tr, new TableLayout.LayoutParams(
        LayoutParams.FILL_PARENT, 
        LayoutParams.FILL_PARENT)); 



      count++; 
     }while(c.moveToNext()); 

    } 

} 

回答

0

我相當肯定你可以簡單地把你的TableLayout放入一個ScrollView來解決這個問題。無需以編程方式添加視圖,只需在XML佈局資源中預定義即可。像這樣:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical"> 

<ScrollView 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

    <TableLayout 
      android:layout_width="wrap_content" 
      android:stretchColumns="0,1,2,3,4,5" 
      android:id="@+id/maintable" 
      android:layout_height="wrap_content" 
      android:layout_weight="1.0"> 

    </TableLayout> 

</ScrollView> 

</LinearLayout> 

請記住,ScrollView是垂直的,所以你需要在那裏添加一個Horizo​​ntalScrollView來添加水平滾動。