36

以下は、私の LAND 形式の XML の一部です。

<TableLayout
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center"
    android:stretchColumns="*">
<TableRow>    
    <Button
        android:id="@+id/countbutton"
        android:text="@string/plus1"/>      
    <Button
        android:id="@+id/resetbutton"
        android:text="@string/reset" 
        />  
</TableRow>
</TableLayout>

そして今、私が得られないもの-1行とボタンのWIDTHは、ボタン内のTEXTに依存します。両方のテキストが同じ長さの場合は、次のように言いましょう: TEXT OK - テーブルの半分は画面の中央にあります。しかし、それらのサイズが異なる場合 - 「A」と「これは長いボタン」としましょう。テーブルの中心は画面の中央にないため、ボタンの幅は等しくありません...

4

6 に答える 6

91

ボタンが同じサイズの行にボタンを配置するには、行う必要があります。

    <LinearLayout android:orientation="horizontal" 
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">
         <Button android:layout_weight="1" 
             android:layout_height="wrap_content" 
             android:layout_width="0dip"/>
         <Button android:layout_weight="1" 
             android:layout_height="wrap_content" 
             android:layout_width="0dip"/>
    </LinearLayout>

ボタンのその他の xml プロパティを入力します。

魔法は、layout_weight および width プロパティにあります。テーブル レイアウトは必要ありません。これらのプロパティは、ビューが親レイアウトで同じスペースを占有する必要があることをレイアウトに伝えます。

于 2010-05-19T12:44:26.520 に答える
4

受け入れられた答えに加えて:

同じ列幅のグリッドに複数の画像が必要な同様の問題があったため、テーブル レイアウトを使用しました。それは機能しましたが、画像が非同期で読み込まれるため、すべての列に少なくとも 1 つの画像が含まれるまで、対応する列が幅全体を占めることになります。

私はRobby Pondのソリューションを使用してこれを解決しましたが、他の行と同じ数の画像があるとは限らない最後の行では機能しませんでした.上と同じ列。これに対抗するために、その行の残りの空の列を通常の View オブジェクトで埋めました。

他のすべての画像と同じレイアウト パラメータを使用します。

width = 0, weight = 1.そしてそれはそれを解決しました!

于 2012-07-27T17:22:12.390 に答える
4

良い例 (元はhttp://androidadvice.blogspot.com/2010/10/tablelayout-columns-equal-width.htmlから)

テスト済みで動作中:

<TableRow>
  <!-- Column 1 -->
  <TextView
     android:id="@+id/tbl_txt1"
     android:layout_width="0dip"
     android:layout_height="wrap_content"
     android:background="@color/red"
     android:textColor="@color/white"
     android:padding="10dip"
     android:layout_margin="4dip"
     android:layout_weight="1"
     android:text="Column 1" />

  <!-- Column 2 -->
  <TextView
     android:id="@+id/tbl_txt2"
     android:layout_width="0dip"
     android:layout_height="wrap_content"
     android:background="@color/red"
     android:textColor="@color/white"
     android:padding="10dip"
     android:layout_margin="4dip"
     android:layout_weight="1"
     android:text="Column 2" />

  <!-- Column 3 -->
  <TextView
     android:id="@+id/tbl_txt3"
     android:layout_width="0dip"
     android:layout_height="wrap_content"
     android:background="@color/red"
     android:textColor="@color/white"
     android:padding="10dip"
     android:layout_margin="4dip"
     android:layout_weight="1"
     android:text="Column 3" />
</TableRow>

于 2014-04-17T12:18:53.713 に答える
0

レイアウト スニペット

<TableLayout
    android:id="@+id/tablelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

テーブル内のボタンのレイアウト プロパティをプログラムで設定するコード:

public void addButtons(View view) {
    TableLayout tableLayout = (TableLayout) findViewById(R.id.tablelayout);
    Context context = getApplicationContext();
    tableLayout.removeAllViews();

    for (int rowIndex = 0; rowIndex < ROWS; rowIndex++) {
        TableRow row = new TableRow(context);
        for (int columnIndex = 0; columnIndex < COLUMNS; columnIndex++) {
            Button btn = new Button(context);
            LayoutParams buttonParams = new LayoutParams(0,
                    LayoutParams.WRAP_CONTENT, 1f);
            btn.setLayoutParams(buttonParams);
            row.addView(btn);
        }
        tableLayout.addView(row);
    }
}
于 2013-10-18T18:23:26.077 に答える