以编程方式在表行内生成布局

时间:2018-04-21 08:03:16

标签: android android-layout android-linearlayout android-tablelayout

我尝试在以编程方式生成的表行布局中添加线性布局。线性布局似乎永远不会显示出来。我已尝试在程序中设置宽度和高度,但没有任何帮助。

TableLayout ll = (TableLayout) view.findViewById(R.id.info_table);

TableRow row=new TableRow(getContext());
        row.setBackgroundColor(RED);
        TableLayout.LayoutParams tableRowParams=
                new TableLayout.LayoutParams
                        (TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);

        int leftMargin=20;
        int topMargin=20;
        int rightMargin=20;
        int bottomMargin=10;

        tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
        row.setLayoutParams(tableRowParams);

        TextView address = new TextView(getContext());
        address.setMinimumWidth(320);
        address.setMaxWidth(320);
        address.setText("Rating");



        address.setTypeface(null, Typeface.BOLD);
        address.setTextSize(TypedValue.COMPLEX_UNIT_SP,16);

        row.addView(address);
        View dummyView = new View(getContext());
        LinearLayout.LayoutParams dummyParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,300);
        dummyParams.weight = 1f;
        dummyView.setLayoutParams(dummyParams);
        dummyView.setBackgroundColor(Color.BLACK);            
        row.addView(dummyView);
        row.bringChildToFront(dummyView);

        ll.addView(row,0);

我只能看到行设置的红色,而不是布局设置的黑色。

1 个答案:

答案 0 :(得分:0)

您可以像下面的例子一样,可以在父视图中创建子视图: 我的代码以这种方式工作可能会对您有所帮助。

 LinearLayout linear=new LinearLayout(this);             
            linear.setLayoutParams(new 
            android.view.ViewGroup.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            tableRowParams.addView(linear);