【问题标题】:Programmatically generated table not matching parent width以编程方式生成的表格与父宽度不匹配
【发布时间】:2014-02-28 13:44:40
【问题描述】:

我正在尝试动态生成我的活动布局,但由于某种原因,我无法让我的 TableLayout 或我的 TableRow 与父宽度匹配。我使用的是相对布局,然后我创建了我的 TableLayout 并在将我的 TableLayout 添加到我的 RelativeLayout 之前用行填充它。

这是我的 onCreate:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    context = this;

    RelativeLayout mainLayout = new RelativeLayout(context);
    RelativeLayout.LayoutParams relativeLayout = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT
    );
    mainLayout.setLayoutParams(relativeLayout);

    //Create the signature views
    //createPage(mainLayout);

    setupPage(mainLayout);

    setContentView(mainLayout);
}

这是我的setupPage() 代码:

private void setupPage(RelativeLayout mainLayout)
{
    TableLayout mainTable = new TableLayout(context);
    RelativeLayout.LayoutParams tableLayoutParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
    );
    mainLayout.setLayoutParams(tableLayoutParams);
    mainTable.setBackgroundResource(R.drawable.rectangle);

    //JOB DESCRIPTION title
    mainTable.addView(getRowTitle("JOB DESCRIPTION"));

    //----   END   ----
    mainLayout.addView(mainTable);
}

private TableRow getRowTitle(String text)
{
    TableRow row = new TableRow(context);
    TableLayout.LayoutParams rowLayout = new TableLayout.LayoutParams(
            TableLayout.LayoutParams.MATCH_PARENT,
            TableLayout.LayoutParams.MATCH_PARENT
    );
    row.setLayoutParams(rowLayout);
    row.setBackgroundResource(R.drawable.rectangle);//so i can see the outline of the row

    /*TextView title = new TextView(context);
    TableRow.LayoutParams editLayout = new TableRow.LayoutParams(
            TableRow.LayoutParams.WRAP_CONTENT,
            TableRow.LayoutParams.WRAP_CONTENT
    );
    editLayout.setMargins(15,0,0,0);
    title.setLayoutParams(editLayout);
    title.setText(text);

    row.addView(title);*/

    return row;
}

【问题讨论】:

  • 您将tableLayoutParams(类型为TableLayout.LayoutParams)的父RelativeLayout 设置为LayoutParams 是否有原因?
  • 我认为孩子必须拥有父母的布局参数?我试过使用 TableLayout.LayoutParams 但这并没有改变任何东西
  • 上面评论中的一个小错误,而不是在mainLayout上设置LayoutParamssetupPage()方法中的mainLayout.setLayoutParams(tableLayoutParams);)设置在实际的TableLayout上:@987654333 @
  • 啊哈。这就是问题所在。我不小心得到了mainLayout.setLayoutParams 而不是mainTable.setLayoutParams .........你能把它作为答案发布以便我接受吗?谢谢你发现我的愚蠢哈哈

标签: android android-relativelayout tablerow android-tablelayout


【解决方案1】:

您没有在正确的视图上设置LayoutParams(很可能以默认的LayoutParams 结束)。在setupPage() 方法中,您可以:

mainLayout.setLayoutParams(tableLayoutParams);

而不是将其设置在TableLayout:

mainTable.setLayoutParams(tableLayoutParams);

【讨论】:

    【解决方案2】:

    试试这个 -

    private void setupPage(RelativeLayout mainLayout)
    {
    TableLayout mainTable = new TableLayout(context);
    RelativeLayout.LayoutParams tableLayoutParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
    );
    mainTable.setLayoutParams(tableLayoutParams);
    mainTable.setBackgroundResource(R.drawable.rectangle);
    
    //JOB DESCRIPTION title
    mainTable.addView(getRowTitle("JOB DESCRIPTION"));
    
    //----   END   ----
    mainLayout.addView(mainTable);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-07
      • 2014-08-21
      • 1970-01-01
      相关资源
      最近更新 更多