16

RelativeLayoutベースである a でいくつかのビューを水平方向に中央揃えにしようとしています。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    android:background="@android:color/transparent" >

これは機能していません。ビューの1つに設定centerInParenttrueましたが、うまくいきました。ただし、中央に配置する必要がある2つのビューが並んでいるため、このソリューションは使用できません。これを最適化しようとしているので、レイアウト、特に線形を互いに入れ子にすることを避けたいと考えています。

私が行方不明であることは明らかですか?この属性はこの状況のた​​めに作られていると思いました。

4

4 に答える 4

14

ネストされた ViewGroup を使用せずに 3 つのビューを含む同様の問題に回答しました。

https://stackoverflow.com/a/13279846/1011746

これは API 11 でテストされています。

2 つのビューが水平の場合:

<RelativeLayout
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:gravity="center"
  android:background="@android:color/black"
  >
  <Button
    android:id="@+id/apply"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="APPLY"
    android:textSize="20sp"
    />
  <Button
    android:id="@+id/undo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="UNDO"
    android:textSize="20sp"
    android:layout_toRightOf="@id/apply"
    />
</RelativeLayout>

2 つのビューの垂直の場合:

<RelativeLayout
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:gravity="center"
  android:background="@android:color/black"
  >
  <Button
    android:id="@+id/apply"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="APPLY"
    android:textSize="20sp"
    />
  <Button
    android:id="@+id/undo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="UNDO"
    android:textSize="20sp"
    android:layout_below="@id/apply"
    />
</RelativeLayout>
于 2012-11-07T23:46:00.857 に答える
10

複数のレイアウトを一緒にネストする必要があります。RelativeLayout で何かを中央に配置するandroid:layout_centerInParent="true"には、子で使用します。複数の子を中央に配置しようとすると、それらは互いに下/上になります。

したがって、たとえば、2 つのビューを持つ LinearLayout を RelativeLayout の子として使用できます。LinearLayout には と がandroid:orientation="horizontal"ありandroid:layout_centerInParent="true"ます。LinearLayout が RelativeLayout の中央に配置され、2 つの子が隣り合っている必要があります。

于 2012-06-05T21:06:16.877 に答える
2

2 つのビューを LinearLayout でラップし、単一の TextView の場合と同様に、LinearLayout を RelativeLayout の中央に配置します。

于 2012-06-05T21:07:16.583 に答える
1

したがって、この問題に対する私の修正は、textview の複合描画可能機能を活用するためだけのものであることが判明しました。ボタンをゴミ箱に移動し、drawableRight を使用して検索アイコンを表示しました。

于 2012-06-06T14:21:01.643 に答える