这个 Android 片段在做什么?

What is this Android Snippet doing?

编辑** 找出我自己的答案,除了我的主要问题如下:

            TableLayout tableLayout = new TableLayout(this);

            TableRow row = new TableRow(getApplicationContext());
            row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT));
            // inner for loop
            for (int j = 1; j <= courseHoleCount; j++) {
                 **TextView tv = new TextView(getApplicationContext());
                tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
                        TableRow.LayoutParams.WRAP_CONTENT));**
                tv.setBackgroundResource(R.drawable.cell_shape);
                tv.setTextSize(30);
                tv.setPadding(5, 5, 5, 5);
                tv.setText(String.valueOf(players[i-1].getScore()[j-1]));// (-1) because j = 1 and not 0.
                row.addView(tv);
            }
            tableLayout.addView(row);


            linearLayout.addView(tableLayout);

在上面的代码片段中,这是做什么的?what/why TableRow.Layout 参数被添加到文本视图布局参数中?

tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
                    TableRow.LayoutParams.WRAP_CONTENT));

TextView 的父级是 TableRow,因此 TextView 的布局参数应该是 TableRow.LayoutParams.

类型

这与指定完全相同:

<TableRow ...>
    <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" />
</TableRow>

如果 TextViewLinearLayout 一样被添加到其他 ViewGroup,那么它需要 LinearLayout.LayoutParams 设置。

TableRow.LayoutParams 被使用是因为 tvrow 的子代,后者是 TableRow。 如果您查看 Android API reference,它会显示

Set the layout parameters associated with this view. These supply parameters to the parent of this view specifying how it should be arranged.

LayoutParams构造函数的两个参数用于设置tv的宽度和高度。由于宽度和高度都设置为WRAP_CONTENTtv will be big enough to enclose its content (plus padding).

的大小