2011-11-12 53 views
0

我需要創建一個適合數據行的動態表格。我想會有一個具體形式中的行,像這樣:如何在代碼中編寫relativeLayouts

http://i.imgur.com/1D8ge.png

因此,爲了得到它,我用relativeLayouts。問題是我第一次嘗試使用.xml來查看哪些參數具有它形成的不同視圖,並且我知道它,但現在我無法在代碼中複製它。

這裏是我的代碼:

TableLayout linkRoutesTable = (TableLayout) findViewById(R.id.layoutLinkRoutes); 

//ROW: 
TableRow linkRouteRow = new TableRow(this); 
linkRouteRow.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
linkRouteRow.setId(i)     

//RELATIVELAYOUT: 
float px0 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, r.getDisplayMetrics()); 
RelativeLayout relativeLayout = new RelativeLayout(this); 
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(0, (int)px0))); 

//CONTENT: 

// BUTTON: ...    
// NUMBER:... 
// ADDRESS:... 
// NAME: ...     

relativeLayout.addView(number); 
relativeLayout.addView(name); 
relativeLayout.addView(address); 
relativeLayout.addView(button); 

linkRouteRow.addView(relativeLayout); 
linkRouteTable.addView(linkRouteRow); 
linkRoutesTable.refreshDrawableState(); 

它不會顯示任何東西......

的.XML顯示OK,那就是:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_gravity="center" android:orientation="vertical"> 

    <TableRow> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 


        android:layout_width="0dp" 
        android:layout_weight="1" 
    android:id="@+id/layoutLinkRoutes" > 


    <Button android:text="Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button1" android:layout_alignParentTop="true" android:layout_alignParentRight="true"></Button> 
    <TextView android:textStyle="bold" android:textSize="45dp" android:text="1" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_marginLeft="22dp"></TextView> 
    <TextView android:text="TextView" android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/button1" android:layout_toRightOf="@+id/textView1" android:layout_marginLeft="16dp"></TextView> 
    <TextView android:text="TextView" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_alignParentTop="true" android:layout_alignLeft="@+id/textView3"></TextView> 

</RelativeLayout> 

</TableRow> 

我在做什麼代碼中出現錯誤?我需要一些幫助,我嘗試了一切。

如果我不使用relativeLayouts,行,它顯示的東西,但壞...所以這個問題是行和RelativeLayout的

真的感謝...

回答

1

您不必在代碼中定義佈局以動態填充表格。您可以使用您在xml中定義的佈局,像正常佈局一樣。

因此,您可以在xml文件中一次性定義您的表格行佈局,並將其多次浸入您的表格佈局中。

檢查this answer

+0

我以前看到過,但我認爲有一個問題:在我的.xml中,我使用了id來對齊視圖(視圖依賴於其他視圖,並用id指出它)...所以如果我膨脹它,它是否會重複ID,每當我膨脹它時,它是否會與新ID一致?對不起,我的壞英語,希望你明白我想說的。 – Frion3L

+0

這個ID不應該成爲一個問題,否則膨脹視圖的整個概念將是無用的。 – Flo