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



我正在尝试在编程生成的表行布局中添加线性布局。线性布局似乎从未显示。我尝试设置程序中的宽度和高度,但没有任何帮助。

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);

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

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

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

最新更新