[英]How can I get an Android TableLayout to fill the screen?
我正在與Android糟糕的布局系統作斗爭。 我正試圖讓桌子填滿屏幕(簡單吧?)但是這太難了。
我讓它以某種方式在XML中工作,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent">
<TableRow android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="1">
<Button android:text="A" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/>
<Button android:text="B" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/>
</TableRow>
<TableRow android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="1">
<Button android:text="C" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/>
<Button android:text="D" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/>
</TableRow>
但是我不能讓它在Java中工作。 我已經嘗試了一百萬種LayoutParams組合,但沒有任何效果。 這是我最好的結果,它只填充屏幕的寬度,而不是高度:
table = new TableLayout(this);
// Java. You suck.
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
table.setLayoutParams(lp); // This line has no effect! WHYYYY?!
table.setStretchAllColumns(true);
for (int r = 0; r < 2; ++r)
{
TableRow row = new TableRow(this);
for (int c = 0; c < 2; ++c)
{
Button btn = new Button(this);
btn.setText("A");
row.addView(btn);
}
table.addView(row);
}
顯然Android文檔沒有幫助。 有人有主意嗎?
上面的討論有兩個錯誤。
可以通過指定TableLayout.LayoutParams
和TableRow.LayoutParams
並使用適當的構造函數以編程方式設置權重,例如
TableLayout.LayoutParams rowInTableLp = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1.0f);
窗口小部件必須具有其父窗口的LayoutParams
。 因此,行必須使用TableLayout.LayoutParams
。
這為您提供了以下初始代碼的工作版本:
TableLayout table = new TableLayout(this);
// Java. You succeed!
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
table.setLayoutParams(lp);
table.setStretchAllColumns(true);
TableLayout.LayoutParams rowLp = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT,
1.0f);
TableRow.LayoutParams cellLp = new TableRow.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT,
1.0f);
for (int r = 0; r < 2; ++r)
{
TableRow row = new TableRow(this);
for (int c = 0; c < 2; ++c)
{
Button btn = new Button(this);
btn.setText("A");
row.addView(btn, cellLp);
}
table.addView(row, rowLp);
}
setContentView(table);
感謝Romain Guy對Android開發人員解決方案論壇的評論。
最后弄清楚如何做到這一點。 放棄TableLayout
並在垂直方向內使用水平LinearLayout
。 關鍵的關鍵是設定重量。 如果指定FILL_PARENT
但使用默認權重,則它不起作用:
LinearLayout buttonsView = new LinearLayout(this);
buttonsView.setOrientation(LinearLayout.VERTICAL);
for (int r = 0; r < 6; ++r)
{
LinearLayout row = new LinearLayout(this);
row.setOrientation(LinearLayout.HORIZONTAL);
for (int c = 0; c < 4; ++c)
{
Button btn = new Button(this);
btn.setText("A");
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
lp.weight = 1.0f;
row.addView(btn, lp);
}
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
lp.weight = 1.0f;
buttonsView.addView(row, lp);
}
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
setContentView(buttonsView, lp);
找到答案:顯然是layout_weight使它工作,沒有辦法從Java設置它。 該死的。
你永遠不會設置行或按鈕布局參數,而在發布的xml中,你這樣做..更改for循環的細節以設置行布局參數和按鈕布局參數,而不是它應該給出與xml相同的結果。
要設置TableLayout LayoutParams,我們邏輯上期望使用TableLayout.LayoutParams類,但是您將收到一個強制轉換錯誤,指出TableLayout.LayoutParams無法轉換為FrameLayout.LayoutParams。
因此,如果要以編程方式設置TableLayout屬性,則應使用FrameLayout.LayoutParams。 例如:
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(80, 0, 0, 0);
TableLayout tableLayout = (TableLayout) findViewById(R.id.header_detail);
tableLayout.setLayoutParams(layoutParams);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.