2012-01-10 190 views
2

我正在使用eclipse + android SDK。動態添加數據行

我正試圖用傳感器提供的數據創建表格。我正確地獲得了這些信息,因爲我在製作一張表格以便以XML格式顯示它時遇到了問題。

這是sensorinfo.XML:

<?xml version="1.0" encoding="utf-8"?> 

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:stretchColumns="1" 
android:id="@+id/SensorInfoTableLayout"> 


<TableRow> 

    <TextView 
      android:layout_column="1" 
      android:text="@string/sensor_name_title" 
      android:padding="3dip" /> 

    <TextView 
      android:layout_column="2" 
      android:text="@string/sensor_type_title" 
      android:padding="3dip" /> 
    <TextView 
      android:layout_column="3" 
      android:text="@string/sensor_value_title" 
      android:padding="3dip" /> 
    <TextView 
      android:layout_column="4" 
      android:text="@string/sensor_unit_title" 
      android:padding="3dip" /> 
</TableRow> 

    <View 
    android:layout_height="4dip" 
    android:background="#FF909090" /> 

</TableLayout> 

這是我的活動代碼:

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

    getSensorInfo(); 
    setContentView(R.layout.sensorinfo); 

    setInfoByView(); 

} 

而且

private void setInfoByView() 
{ 
    /* Find Tablelayout defined in xml */ 
    TableLayout myTableLayout = (TableLayout)findViewById(R.id.SensorInfoTableLayout); 
    /* Create a new row to be added. */ 
    TableRow myTableRow = new TableRow(this); 
    myTableRow.setLayoutParams(new LayoutParams(
         LayoutParams.WRAP_CONTENT, 
         LayoutParams.WRAP_CONTENT)); 
    /* Create a Button to be the row-content. */ 
    Button b = new Button(this); 
    b.setText("Dynamic Button"); 
    b.setLayoutParams(new LayoutParams(
         LayoutParams.WRAP_CONTENT, 
         LayoutParams.WRAP_CONTENT)); 
    myTableRow.setBackgroundColor(5); 
    /* Add Button to row. */ 
    myTableRow.addView(b); 
    /* Add row to TableLayout. */ 
    myTableLayout.addView(myTableRow,new TableLayout.LayoutParams(
       LayoutParams.WRAP_CONTENT, 
       LayoutParams.WRAP_CONTENT)); 
} 

這是一個測試,我是試圖包括一個按鈕dinamically,如果工作,我會把我從getSensorInfo()獲得的信息;

但我的活動是這樣的:

ACTIVITY IMAGE

什麼可能我是做錯了什麼?

我發現的所有信息是THIS,但答案並沒有幫助我。

回答

4

首先建立在XML文件中的一個表佈局是這樣的:

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

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

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

</ScrollView> 

</LinearLayout> 

然後使用這個代碼表佈局內創建動態行:

  TableLayout image_table=null; 
     image_table=(TableLayout)findViewById(R.id.image_table); 

     for(int i=0; i<10; i++){ 
      TableRow tableRow=new TableRow(this); 
      tableRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
      tableRow.setGravity(Gravity.CENTER_HORIZONTAL); 
      tableRow.setPadding(5, 5, 5, 5); 
      for(int j=0; j<1; j++){ 
       Button bttn=new Button(this); 
       bttn.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
       bttn.setText(Hello); 
       tableRow.addView(image, 200, 200); 
      } 
      image_table.addView(tableRow); 
     } 

FINAL(測試)解決方案:

private void setInfoByView2() 
{ 
    TableLayout myTableLayout = null; 
    TableRow myTableRow = new TableRow(this); 
    myTableLayout = (TableLayout)findViewById(R.id.SensorInfoTableLayout); 

    myTableRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
    //myTableRow.setGravity(Gravity.CENTER_HORIZONTAL); 
    myTableRow.setPadding(5, 5, 5, 5); 
    Button bttn = new Button(this); 
    bttn.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
    bttn.setText("Hello"); 
    myTableRow.addView(bttn, 200, 200); 
    myTableLayout.addView(myTableRow); 
} 

並且該按鈕現在是可見的。

+0

感謝您的幫助,調整您的代碼,我的作品! – vgonisanz 2012-01-10 14:11:53

+0

@Piperoman永遠歡迎。享受! – Venky 2012-01-10 14:14:10