28

私は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のドキュメントは役に立ちません。誰かアイデアはありますか?

4

5 に答える 5

35

上記の議論には2つの間違いがあります。

  1. 適切なコンストラクターを指定TableLayout.LayoutParamsして使用することにより、プログラムで重みを設定することができます。TableRow.LayoutParams

    TableLayout.LayoutParams rowInTableLp = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1.0f);
    
  2. ウィジェットには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);

ソリューションに関するAndroid開発者フォーラムへのRomainGuyのコメントに感謝します。

于 2011-01-10T12:32:35.373 に答える
8

最後にこれを行う方法を考え出しました。あきらめて、垂直のものの中で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);
于 2010-03-20T00:36:16.337 に答える
1

答えが見つかりました:明らかにそれを機能させるのはlayout_weightであり、Javaから設定する方法はありません。くそ。

ランドスケープモードで親を埋めるためにAndroidTableLayoutを取得するにはどうすればよいですか?を参照してください。

于 2010-03-08T11:24:40.427 に答える
0

行またはボタンのレイアウトパラメーターを設定することはありませんが、投稿されたxmlではそれを行います。forループの詳細を変更して、行レイアウトパラメーターとボタンレイアウトパラメーターの両方を、xmlと同じ結果になるように設定します。

于 2010-03-06T20:35:45.777 に答える
0

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);
于 2016-11-04T09:07:04.137 に答える