私は1行と1つのセルだけを含むTableLayoutを持っています。 私はセルのコンテンツを水平に配置したいと思います。 私は2つのアプローチを試みた:のTableRow要素に重力=「center_horizontal」と期待どおりに動作します:Android TableLayoutと水平方向の重さ
まず方法を、私は属性アンドロイドを設定
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow android:gravity="center_horizontal">
<Button
android:id="@+id/MyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Hello" />
</TableRow>
</TableLayout>
第二の方法、私は属性を設定しましたアンドロイド:セルの内容(ここではボタン)に直接layout_gravity =「center_horizontal」それがボタンを中心としません:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<Button
android:id="@+id/MyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/Hello" />
</TableRow>
</TableLayout>
誰かが2番目のアプローチがうまくいかない理由を説明できますか?
Riana
こんにちは、あなたの答えに感謝:
は、他のいくつかの例や、さらなる説明のために、この同様の質問を参照してください。私の例では、最初のアプローチがなぜ機能するのか理解しています。明らかにされていないのは、2番目のものが失敗する理由です。ボタンに適用されたlayout_gravity属性は、親ビュー(TableRow)の中にボタンを配置する必要があります。 –
これは明確ではありません。 Gravityは、テーブルセルの子コンテンツ用です。 Layout_gravityは、親にテーブルセル自体を配置するためのものです。ちょうどその答えで説明されています。 – joshgoldeneagle
私の例では、ButtonはTableRowの子です。 "重力"属性をTableRowに設定すると、その内容が中央に配置されます(ここではボタン)。 しかし、私はButtonに "layout_gravity"属性を設定すると、そのコンテナの内部、つまりTableRowの中央に置くことも期待しています。 コンテナの「重力」を設定するか、コンテンツのlayout_gravityを同じに設定する必要があります。 私はより明確であることを願っています。 –