TableRow内のビューに対してlayout_gravity
を指定するとlayout_width
の設定がうまく効かない場合がありました。
LinearLayoutなどで同様にしてもlayout_width
はちゃんと効きます。
ActivityのXMLを以下のようにすると、
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TableRow" />
<EditText
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="end" />
</TableRow>
<LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LinearLayout" />
<EditText
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="end" />
</LinearLayout>
</TableLayout>
EditTextの中身は同じですが、TableRowのほうのEditTextはとても短いです・・。
(hintなどを入れると文字数分の長さになるので、layout_width="wrap_content"
を指定したときのような動きになっていそうです)
なぜこうなるのかはよくわかりませんでしたが・・
TableRow内のビューにlayout_width
を設定しつつ右寄せしたい場合などは、ビューをLayoutで囲んで、Layoutに対してlayout_gravity
を指定するとうまくいくようです。
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TableRow" />
<LinearLayout
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="end" >
<EditText
android:layout_width="100dp"
android:layout_height="wrap_content" />
</LinearLayout>
</TableRow>
</TableLayout>