2013-07-01 198 views
0

我想了解如何有人做一個TableView而不指定它在XML文件中。我有兩個活動:在水平滾動視圖中添加動態表視圖

  1. 注意到行的列數和
  2. 打造一個TableView中給定的行和列(該電池0之間充滿了隨機數 - 2)

所以,這一個工作很好,但是當我試圖把太多列,顯然這些數字幾乎沒有看到。

第二個活動:

public class TableCreator extends Activity { 

    private String getRandomPoints(){ 
     Random rn = new Random(); 

     return new Integer(Math.abs(rn.nextInt()%3)).toString(); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.table_creator); 

     Bundle extras = getIntent().getExtras(); 
     int rows = extras.getInt("rows"); 
     int columns = extras.getInt("columns"); 


     TableLayout table = (TableLayout) findViewById(R.id.TableLayout); 
     for(int i=0;i<rows;i++){ 
      TableRow row = new TableRow(this); 
      row.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)); 

      for(int j=0;j<columns;j++){ 
       TextView tv = new TextView(this); 
       tv.setText(getRandomPoints()); 
       tv.setLayoutParams(new TableRow.LayoutParams(0,LayoutParams.WRAP_CONTENT,1)); 
       tv.setGravity(Gravity.CENTER); 

       row.addView(tv); 
      } 
      table.addView(row); 
     } 
    } 
} 

和XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/TableLayout" 
    android:stretchColumns="0,1" 
    android:layout_weight="1"  
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
</TableLayout> 

之後,我試圖在XML文件中添加Horizo​​ntalScrollView,所以TableView中是內它。這是一個例外,因爲系統不能決定excact寬度的高度,TableLayout將使用...

我也嘗試以編程方式添加Horizo​​ntalScrollView,但數字沒有顯示出來。我可以說,我在這個網站上仔細搜索了同樣的問題,但仍然有點不相關,我的答案...

回答

0

在這裏你可以設置佈局爲水平滾動視圖和tablelayout是孩子

<HorizontalScrollView 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" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".EvacRouteTableActivity" > 

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

    <TableRow 
     android:id="@+id/TableRow01" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
    <TextView 
     android:id="@+id/TextView01" 
     android:background="@drawable/cell_shape" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text=" Evacuation Routes (To view route click name)" 
     android:width="340dp" 
     android:height="30dp" 
     android:textStyle="bold"></TextView> 

    </TableRow> 

</TableLayout> 
+0

感謝您的回答,但我不能看你怎麼解釋我什麼,我問......我想明白了,一個Horizo​​ntalScrollView可與動態添加數據的方式......你提出的形狀我是有幫助的,但對於另一個問題... –

+0

好吧改變了我的答案抱歉。 – yams

+0

再次感謝發佈,但我再次說textviews添加programmaticlly。 「非動態」的方式很簡單,可以立即完成...爲了節省每個人的時間,我找到了解決方案。在某種程度上,這個問題似乎並不是因爲添加了scrollView,但我添加LayoutParameters的方式是錯誤的......無論如何,請參閱[link](http://stackoverflow.com/questions/11963465/android-layoutparams-for-textview-makes-the-view-disappear-programatically) –