2015-07-21 159 views
0

我正在使用垂直滾動視圖的表格佈局來顯示數據programmaticlly的巨大列表。在平板電腦上,它的工作原理是一種完美的方式,但是當我在諸如銀河或連線等手機上運行時會出現問題1表格佈局的寬度與屏幕寬度不匹配,因此隱藏了兩列或三列我嘗試了所有組合match_parent,WRAP_CONTENT ......我用了一個水平滾動查看它的工作原理,但我不認爲這是一個很好的解決方案 這是XML文件:android表格佈局不適合屏幕

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context="..." 
    > 
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/mv_table" 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" 
     android:layout_marginTop="50dp"> 
    </TableLayout> 
</RelativeLayout> 
</ScrollView> 

這是我的代碼:

TableLayout mvtable = (TableLayout) findViewById(R.id.mv_table); 
     TableRow tr_head = new TableRow(this); 
     tr_head.setId(10); 
     tr_head.setBackgroundColor(Color.rgb(24,65,95)); 
     TextView label_Date = new TextView(this); 
     label_Date.setId(20); 
     label_Date.setText("Date"); 
     label_Date.setTextColor(Color.WHITE); 
     label_Date.setPadding(5, 5, 5, 5); 
     tr_head.addView(label_Date); 

     TextView label_Debit = new TextView(this); 
     label_Debit.setId(21); 
     label_Debit.setText("Débit"); 
     label_Debit.setTextColor(Color.WHITE); 
     label_Debit.setPadding(5, 5, 5, 5); 
     tr_head.addView(label_Debit); 

..... 

    tr_head.setLayoutParams(new TableRow.LayoutParams(
       TableRow.LayoutParams.FILL_PARENT, 
       TableRow.LayoutParams.WRAP_CONTENT)); 
     mvtable.addView(tr_head, new TableLayout.LayoutParams(
       TableLayout.LayoutParams.FILL_PARENT, 
       TableLayout.LayoutParams.WRAP_CONTENT)); 

     //setting data programmaticlly 
     int c ; 
     int i=1; 

     for (Movement mv : movementsList) 
     { 

      TableRow tr = new TableRow(this); 
      if (i % 2 != 0) 
      { 
       c=Color.WHITE; 

       tr.setBackgroundColor(Color.GRAY);} 
      else 
      {c=Color.BLACK; 
      } 
      tr.setId(100 + i); 
      tr.setLayoutParams(new TableRow.LayoutParams(
        TableRow.LayoutParams.FILL_PARENT, 
        TableRow.LayoutParams.WRAP_CONTENT)); 



      TextView labelDate = new TextView(this); 
      labelDate.setId(200+i); 
      labelDate.setText(mv.getDate()); 
      labelDate.setTextColor(c); 
      labelDate.setPadding(5, 5, 5, 5); 
      tr.addView(labelDate); 


      TextView labelDebit = new TextView(this); 
      labelDebit.setId(210+i); 
      labelDebit.setText(mv.getDebit()); 
      labelDebit.setTextColor(c); 
      labelDebit.setPadding(5, 5, 5, 5); 
      labelDebit.setBackgroundColor(Color.rgb(255,174,185)); 
      tr.addView(labelDebit); 

...... 



      tr.setLayoutParams(new TableRow.LayoutParams(
        TableRow.LayoutParams.FILL_PARENT, 
        TableRow.LayoutParams.WRAP_CONTENT)); 

      mvtable.addView(tr, new TableLayout.LayoutParams(
        TableLayout.LayoutParams.FILL_PARENT, 
        TableLayout.LayoutParams.WRAP_CONTENT)); 
      i++; 
     } 

回答

0

使用LinearLayout而不是RelativeLayout它可能會解決您的問題。因爲RelativeLayout重疊在其他

+0

做到了,但相同 – elpazio