2016-01-25 49 views
0

我使用下面的代碼progmatically按鈕添加到基於項目的活動從JSON響應收到:Android的AddRule按鈕在TableLayout/TableRow?

TableLayout layout_events = (TableLayout) findViewById(R.id.layout_events); 

JSONArray json = new JSONArray(output); 

for (int i = 0; i < json.length(); i++) 
{ 
    JSONObject object = json.getJSONObject(i); 
    JSONObject tournament = object.getJSONObject("tournament"); 

    Button button = new Button(this); 
    button.setSingleLine(true); 
    button.setText(tournament.get("name").toString()); 

    TableRow row = new TableRow(this); 
    row.addView(button); 

    layout_events.addView(row); 
} 

此工作的罰款。不過,我希望按鈕能夠拉伸整個表格的寬度。我將如何完成這項工作?

我試圖沿着線的東西:

TableRow.LayoutParams layout = new TableRow.LayoutParams(
    TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT); 

button.setLayoutParams(layout); 

但這既然TableRow.LayoutParams沒有addRule()功能不起作用。

任何人有任何想法?

回答

1

android:stretchColumns="*"置於您的TableLayout

您可以以編程方式爲按鈕添加權重1。

TableRow.LayoutParams layout = new TableRow.LayoutParams(
    TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT,1f); 

您必須以編程方式設置ellipsize。

button.setEllipsize(TextUtils.TruncateAt.END); 
+0

那種作品...但是然後按鈕不正確地尊重「setSingleLine」選項。是的,他們是單線,但他們沒有用最大寬度的橢圓切斷......反而他們伸出了佈局。 –

+0

這很有趣@JasonAxelrod – KDeogharkar

+0

看到我更新的答案@JasonAxelrod – KDeogharkar