2014-10-06 33 views
0

我的長期目標是在Android應用程序中創建一個可點擊的表格,該表格是根據類別對象列表動態製作的。我的短期目標是弄清楚如何獲得表格。現在我已經在設計視圖中定義了一個TableLayout,並試圖以動態/編程方式填充它。無法在Android中顯示錶格

代碼:

XML文件:

<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:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context="com.example.nick.timelogger.itemTable" 
android:background="#ffffffff"> 

    <TableLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true" 
     android:id="@+id/itemTable"> 

    </TableLayout> 
</RelativeLayout> 

類 - onCreate方法(現在我只是想添加一行把它顯示在我的應用程序):

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

    RelativeLayout.LayoutParams tableParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT); 

    TableLayout itemTable = (TableLayout)findViewById(R.id.itemTable); 
    itemTable.setLayoutParams(tableParams); 

    TableRow header = new TableRow(getApplicationContext()); 
    header.setLayoutParams(tableParams); 

    TextView itemName = new TextView(getApplicationContext()); 
    itemName.setText("Activity Name"); 
    itemName.setTextColor(Color.BLACK); 
    itemName.setTextSize(16f); 
    itemName.setTypeface(Typeface.DEFAULT); 

    TextView status = new TextView(getApplicationContext()); 
    status.setText("Status"); 
    status.setTextColor(Color.BLACK); 
    status.setTextSize(16f); 
    status.setTypeface(Typeface.DEFAULT);; 

    TextView elapsed = new TextView(getApplicationContext()); 
    elapsed.setText("Elapsed Time"); 
    elapsed.setTextColor(Color.BLACK); 
    elapsed.setTextSize(16f); 
    elapsed.setTypeface(Typeface.DEFAULT); 

    header.addView(itemName,(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT))); 
    header.addView(status,(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT))); 
    header.addView(elapsed,(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT))); 

    itemTable.addView(header); 
} 

我錯過了什麼?

回答

0

我發現我的代碼中的兩個問題:

1)首先我被添加tableparams爲rowparams 2)第二我使用了不同形式的「header.addView」,通過取出所述第二參數,並這似乎工作。

看起來Android的變化太快了,以至於將代碼從舊的堆棧中解出並不總是很正確。