如何在代码中编写relativeLayouts

时间:2011-11-12 18:46:10

标签: android layout android-activity

我需要创建一个适合数据行的dinamic表。我想要的行将有一个特定的形式,如下所示:

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显示确定,这是:

<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 个答案:

答案 0 :(得分:1)

您不必在代码中定义布局以动态填充表格。您可以像在普通布局中那样使用xml定义的布局进行渗透。

因此,您在xml文件中定义了一次表格行布局,并将其多次渗透到表格布局中。

检查this answer