【问题标题】:Generate Layout Inside a Table Row Programmatically以编程方式在表格行内生成布局
【发布时间】:2018-04-21 08:03:16
【问题描述】:

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

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

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2023-03-16
      • 2023-03-15
      • 1970-01-01
      • 2015-02-08
      • 1970-01-01
      • 2014-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多