动态表格布局

时间:2012-07-22 14:50:07

标签: android tablelayout

我想在tablerow中显示来自我的数据库的一些注释,从数据库中获取带有注释的数据是正常的,但我的活动中没有任何内容显示我的页面标题,我只是找不到错误:S

这是java部分:

public class PIComments extends Activity{

      public static final String EXTRA_IDPI="EXTRA_IDPI";
      TableLayout messagesTable;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.picomments);
            messagesTable = (TableLayout)findViewById(R.id.commentTable);

            long PIID = getIntent().getLongExtra(EXTRA_IDPI, 0);

            if(PIID>0){

                ArrayList<CommentMsg> comments = CommentMsg.getPIComments(PIID);

                if(comments != null){

                    for(int i=0; i<comments.size(); i++) {

                        // Create a TableRow and give it an ID
                        TableRow tr = new TableRow(this);
                        tr.setId(i);
                        tr.setLayoutParams(new LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.WRAP_CONTENT));   

                        // Create a TextView to house the name of the province
                        TextView login = new TextView(this);
                        login.setId(i);
                        login.setText(comments.get(i).getMsg());
                        //login.setText(comments.get(i).getAuteur());
                        login.setTextColor(Color.BLACK);
                        login.setLayoutParams(new LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.WRAP_CONTENT));
                        tr.addView(login);

                        // Add the TableRow to the TableLayout
                        messagesTable.addView(tr, new TableLayout.LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.WRAP_CONTENT));

                    }
                }
                else{
                    Dialog d = new Dialog(PIComments.this);
                    d.setTitle("No comments for the moment ... ");
                    d.show();
                }
            }

        }

}

并且有xml:

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:background="#ffb0b6"
        android:text="Comments"
        android:textColor="#b3000d"
        android:textSize="26dp"
        android:textStyle="bold" />

    <LinearLayout
        android:id="@+id/ll_country"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <ScrollView
            android:id="@+id/ScrollView11"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:fillViewport="true" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="5dp" >

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

            </LinearLayout>
        </ScrollView>
    </LinearLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

知道了,每个LayoutParams应该是android.widget.TableRow.LayoutParams,除了提供给tl.addView(...)

的一个
/* Find Tablelayout defined in main.xml */
TableLayout tl = (TableLayout) findViewById(R.id.SaleOrderLines);
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
/* Create a Button to be the row-content. */
Button b = new Button(this);
b.setText("Dynamic Button");
b.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
/* Add Button to row. */
tr.addView(b);
/* Add row to TableLayout. */
//tr.setBackgroundResource(R.drawable.sf_gradient_03);
tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));

OR

我有同样的问题,然后我删除

textView.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));

从每个文本视图中显示一些内容。

OR

将textView颜色设置为

textView.setTextColor(Color.WHITE);

您的背景可能会出现黑色和文字颜色:)

仍然没有帮助,请参考这个非常好的教程,你可能会遗漏一些东西。Dynamic TableLayout in Android

我希望这会对你有所帮助。

谢谢:)