0

私の Android アプリには、それぞれが異なるアクティビティを起動する 3 つのボタンを表示するかなり単純なアクティビティがあります。現在、RelativeLayout を使用して中央のボタンを水平方向と垂直方向の両方の中央に配置し、上部と下部のボタンを中央のボタンから 30 dp 離して配置します (水平方向にも中央に配置します)。

ただし、私がやりたいのは、ボタンを画面幅の特定の割合になるように伸ばすことです。これを行う方法とボタンを中央に保つ方法がわかりません。ボタンの両側の LinearLayout で「フィラー」として使用できる適切なオブジェクトはありますか (ウェイトを設定するだけで済みます)。または、LinearLayout を使用せずにこれを行う方法はありますか?

現状のレイアウトの XML は次のとおりです。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button2"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="30dp"
        android:onClick="button1Callback"
        android:text="@string/button1Label" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:onClick="button2Callback"
        android:text="@string/button2Label" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="@string/button3Label" />

</RelativeLayout>
4

1 に答える 1

1

もちろん。ビューまたはフレームの両方が機能します。

<LinearLayout android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <View android:layout_height="0dp"  
    android:layout_width="0dp"
    android:layout_weight="60" />
   <Button android:layout_height="wrap_content"  
    android:layout_width="0dp"
    android:layout_weight="20" />
   <View android:layout_height="0dp"  
    android:layout_width="0dp"
    android:layout_weight="20" />
 </LinearLayout>

スペーサーとしてうまく機能し、私が知る限り、まったく無害であるようです. 私は自分のアプリでこれをかなり使用しています (正直なところ、私のボタンのほとんどは固定幅です)。

ある時点で、プロポーショナル レイアウトのカスタム ビューを実際に作成しました。でも結局全く使わなくなってしまいました。ほとんどの場合、線形レイアウトで慎重にウェイトを適用すると、同等のプロポーショナル レイアウトを得ることができます。

于 2013-01-02T20:57:00.717 に答える