1

私は次のように TableLayout を持っています:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:stretchColumns="1">

<TableRow>
  <Button
     android:id="@+id/b1"
     android:layout_width="0dip"
     android:layout_height="fill_parent"
     android:layout_weight="1"
     android:gravity="center" />
  <Button
     android:id="@+id/b2"
     android:layout_width="0dip"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:gravity="center" />
  <Button
     android:id="@+id/b3"
     android:layout_width="0dip"
     android:layout_height="fill_parent"
     android:layout_weight="1"
     android:gravity="center" />
 </TableRow>
</TableLayout>

各ボタンの幅は同じです。これらのボタンの高さを幅とまったく同じにしたい。私はプログラムでそれをやろうとしました:

Button b1 = (Button) findViewById(R.id.b1);
b1.setHeight(b1.getWidth());

しかし、それは機能しません (値が 0 になりました)。(onCreateメソッド内で)それを行うと、ボタンがまだ設定されていないためだと思います。

4

1 に答える 1

1

まず、ボタンを取得しようとしたときに画面がまだ描画されていないため、0 の値を取得しますwidth

私が見ているように、あなたが望むようにそれを行う唯一の可能性は、XMLファイルで定義済みの値をそれらに与えることです。

例えば:

  <Button
     android:id="@+id/b1"
     android:layout_width="25dip"
     android:layout_height="25dip"
     android:gravity="center" />

プログラムで幅と高さを設定します。

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

btnWidth = metrics.heightPixels/3 - 50;//gap
btnHeight = btnWidth;

Button b1 = (Button) findViewById(R.id.b1);
b1.setHeight(btnWidth);
b1.setWidth(btnWidth);
于 2012-09-28T16:58:56.137 に答える