くらげになりたい。

くらげのようにふわふわ生きたい日曜プログラマなブログ。趣味の備忘録です。

等間隔で配置する(layout_weightとlayout_width)

TableLayoutなどで、等間隔に表示した場合がある。
layout_weightパラメタでうまくいくと思っていたが、微妙にハマったのでメモ。

<TableLayout>
  <TableRow>
    <TextView
        android:id="@+id/fst"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <TextView
        android:id="@+id/snd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
  </TableRow>
</TableLayout>

こんな感じで「layout_weight="1"」を指定したが、TextViewの表示文字数によって、レイアウトがずれてしまう。。。

正しいのは「layout_width="0dip"」を指定することだったらしい。

<TableLayout>
  <TableRow>
    <TextView
        android:id="@+id/fst"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <TextView
        android:id="@+id/snd"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
  </TableRow>
</TableLayout>

「layout_weight」と「layout_width="wrap_content"」を同時に指定すると、「layout_width」が勝手しまうようだ。
「layout_weight」を指定するときには、「layout_width="0dip"」にしよう。