0

我的目标:

我正在尝试使用一堆以编程方式添加的视图填充一个表(带有我在 xml 中创建的标题)。计划是将一些添加TextViews到 aTableRow中,然后将其添加TableRow到创建的最终表中。我尝试按照本教程进行操作。

我的问题:

以编程方式添加的视图不会与 xml 创建的 TextView 规定的列内联。请看下面的截图:

在此处输入图像描述

我的代码:1.) XML

<TableLayout
            android:id="@+id/tableViewTable"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_marginRight="@dimen/margin_EvalProblemTableRight"
                    android:layout_marginBottom="@dimen/margin_EvalProblemTableBottom"
                    android:text="@string/str_EvalProblemNumber"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_marginRight="@dimen/margin_EvalProblemTableRight"
                    android:layout_marginBottom="@dimen/margin_EvalProblemTableBottom"
                    android:text="@string/str_EvalProblemString"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_marginRight="@dimen/margin_EvalProblemTableRight"
                    android:layout_marginBottom="@dimen/margin_EvalProblemTableBottom"
                    android:text="@string/str_EvalProblemCorrectSolution"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_marginRight="@dimen/margin_EvalProblemTableRight"
                    android:layout_marginBottom="@dimen/margin_EvalProblemTableBottom"
                    android:text="@string/str_EvalProblemUserSolution"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_marginRight="@dimen/margin_EvalProblemTableRight"
                    android:layout_marginBottom="@dimen/margin_EvalProblemTableBottom"
                    android:text="@string/str_EvalProblemDuration"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

            </TableRow>            
        </TableLayout>

2.)Java:

public class EvalActivity extends Activity {

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

        TableLayout tableViewTable = (TableLayout) findViewById(R.id.tableViewTable);

        TableRow tr = new TableRow(this);
        tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        Button b = new Button(this);
        b.setText("Button");
        b.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        TextView t1 = new TextView(this);       
        t1.setText("I am textView1");
        t1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        TextView t2 = new TextView(this);       
        t2.setText("I am textView2");
        t2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        TextView t3 = new TextView(this);       
        t3.setText("I am textView3");
        t3.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        TextView t4 = new TextView(this);       
        t4.setText("I am textView3");
        t4.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        tr.addView(b);
        tr.addView(t1);
        tr.addView(t2);
        tr.addView(t3);
        tr.addView(t4);

        tableViewTable.addView(tr,(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)));
    }

如何将“硬编码”xml-TableRow 与以编程方式添加的视图和行一起加入?

4

1 回答 1

0

尝试手动向布局参数添加对齐规则,如下所示:

LayoutParams myParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
myParams.addRule(RelativeLayout.ALIGN_RIGHT, R.id.tableColumnX);

TextView t3 = new TextView(this);       
t3.setText("I am textView3");
t3.setLayoutParams(myParams);
于 2013-02-12T17:17:45.767 回答