2014-11-14 69 views
0

我正在使用表格佈局來顯示基於用戶規格的信息。用戶將數據輸入到數據庫中,然後將這些數據採集並顯示在表格上,每行有3個textView條目。問題是,如果用戶輸入的數據很長,它將從屏幕上消失並且不可見。有任何想法嗎?我曾經做到這一點的代碼如下:tableRow entries off off screen

XML

<TableLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:stretchColumns="*" 
    android:id="@+id/tl"> 
<TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="1dp" > 

     <TextView 
      android:id="@+id/timeCol" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Time" 
      android:textSize="20sp" 
      android:textStyle="bold" 
      android:textColor="#FF0000"/> 

     .... 

    </TableRow>   
</TableLayout> 

的Java

for (final String time : timesArray) 
    { 
     i++; 
     TableRow row1= new TableRow(this); 

     event = db.getEvent(i, gameId); 
     player = db.getPlayer(i, gameId); 

     TextView timeRow = new TextView(this); 
     TextView playerRow = new TextView(this); 
     TextView eventRow = new TextView(this); 

     timeRow.setText(time); 
     timeRow.setTextColor(Color.WHITE); 
     playerRow.setText(player); 
     playerRow.setTextColor(Color.WHITE); 
     eventRow.setText(event); 
     eventRow.setTextColor(Color.WHITE); 

     row1.addView(timeRow); 
     row1.addView(playerRow); 
     row1.addView(eventRow); 

     tl.addView(row1); 
    } 

而且它看起來像這樣如果數據太長enter image description here

+0

設置的空白左,右爲所有的 2014-11-14 16:36:45

+0

首先我很榮幸這樣一個傳奇拳擊手回答!其次,我應該怎樣設定邊距? – Donnacha 2014-11-14 16:46:25

+0

向表格佈局添加一些左右邊距,例如5dp。 android:layout_marginLeft =「5dp」 android:layout_marginRight =「5dp」 – 2014-11-14 16:53:30

回答

1

解決方案!

將layout_width設置爲0,然後根據希望列佔用的屏幕量將layout_weight分配給float(例如,0.5將使列佔據屏幕的一半),從而允許所有信息爲顯示在屏幕上。下面是執行此操作的代碼。

XML

<TableLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/tl"> 

    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="1dp" > 

     <TextView 
      android:id="@+id/timeCol" 
      android:layout_width="0px" 
      android:layout_weight="0.2" 
      android:layout_height="wrap_content" 
      android:text="Time"/> 

     <TextView 
      android:id="@+id/playerCol" 
      android:layout_width="0px" 
      android:layout_weight="0.4" 
      android:layout_height="wrap_content" 
      android:text="Player"/> 

     <TextView 
      android:id="@+id/eventCol" 
      android:layout_width="0px" 
      android:layout_weight="0.4" 
      android:layout_height="wrap_content" 
      android:text="Event"/> 

    </TableRow>   

的Java

TableLayout tl = (TableLayout) findViewById(R.id.tl); 
TableRow row1= new TableRow(this); 

TextView timeRow = new TextView(this); 
TextView playerRow = new TextView(this); 
TextView eventRow = new TextView(this); 

TableRow.LayoutParams text1Params = new TableRow.LayoutParams(); 
     text1Params.width = 0; 
     text1Params.weight = (float) 0.2; 
     TableRow.LayoutParams text2Params = new TableRow.LayoutParams(); 
     text2Params.weight = (float) 0.4; 
     text2Params.width = 0; 
     TableRow.LayoutParams text3Params = new TableRow.LayoutParams(); 
     text3Params.weight = (float) 0.4; 
     text3Params.width = 0; 

     timeRow.setLayoutParams(text1Params); 
     playerRow.setLayoutParams(text2Params); 
     eventRow.setLayoutParams(text3Params); 

     row1.addView(timeRow); 
     row1.addView(playerRow); 
     row1.addView(eventRow); 

     tl.addView(row1, params);