2014-02-28 75 views
1

我想動態生成我的活動佈局,但出於某種原因,我無法讓我的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; 
} 
+0

有,爲什麼你設置爲'LayoutParams'一個理由對於父親'RelativeLayout','tableLayoutParams'(類型爲'TableLayout.LayoutParams')? – Luksprog

+0

我以爲孩子必須有父母的佈局參數?我試過使用TableLayout.LayoutParams,但是這並沒有改變任何東西 – john

+1

上面的註釋中有一個小錯誤,而不是在mainLayout中設置'LayoutParams'('mainLayout.setLayoutParams(tableLayoutParams);''' setupPage()'方法)將它設置在實際的'TableLayout'上:'mainTable.setLayoutParams(tableLayoutParams);' – Luksprog

回答

3

你未在適當的視圖設置LayoutParams(其中最有可能與默認LayoutParams結束)。在setupPage()方法你做:

mainLayout.setLayoutParams(tableLayoutParams); 

,而不是把它放在了TableLayout

mainTable.setLayoutParams(tableLayoutParams); 
+1

謝謝,工作正常... –

1

試試這個 -

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