44

水平方向に同じ量の使用可能なスペースを使用する 3 つのボタンを配置したいと考えています。使用しandroid:layout_gravityました。何が問題ですか?

レイアウト xml :

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="1.0"
    >

    <Button android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Bg"
            android:background="@drawable/button_red"
            android:layout_weight=".2"
            android:layout_gravity="left"
    />
    <Button android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Bm"
            android:background="@drawable/button_red"
            android:layout_weight=".2"
            android:textColor="#ffffff"
            android:layout_gravity="center"
    />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_red"
        android:layout_weight=".2"
        android:text="@string/Bf"
        android:layout_gravity="right"
    />

</LinearLayout>

誰かが間違っているのを見たら、ありがとう。

4

9 に答える 9

120

次のレイアウトが機能するはずです。重みは LinearLayouts 用です。

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3"
>

    <Button android:id="@+id/button1"
            ...
            android:layout_weight="1"/>

    <Button android:id="@+id/button2"
            ...
            android:layout_weight="1"/>

    <Button
        android:id="@+id/button3"
        ...
        android:layout_weight="1"/>

</LinearLayout>
于 2012-07-26T12:32:35.470 に答える
14

a を設定する以外に、またはを設定するlayout_weight必要があります。したがって、たとえば、ボタンを水平方向に分散させたい場合は、0dp と.2、または使用しているボタンで等しい限り、任意の数値にする必要があります。layout_widthlayout_height0dplayout_widthlayout_weight

したがって、レイアウトは次のようになります

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>

<Button android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/Bg"
        android:background="@drawable/button_red"
        android:layout_weight="1"
/>
<Button android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/Bm"
        android:background="@drawable/button_red"
        android:layout_weight="1"
        android:textColor="#ffffff"
/>

<Button
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@drawable/button_red"
    android:layout_weight="1"
    android:text="@string/Bf"
/>
</LinearLayout>
于 2013-03-07T15:09:27.820 に答える
2

すべてのボタンで重量の合計を均等に分割します。

layout_gravity プロパティを削除し、android:layout_weight=0.33 を追加します。

それが動作します

于 2012-07-26T12:24:22.123 に答える
1

これをチェックしてください:

<RelativeLayout 
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <Button 
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"/>
    <RelativeLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true">
        <Button 
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/button1"/>
    </RelativeLayout>
    <Button 
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"/>
</RelativeLayout>
于 2012-07-26T12:18:17.100 に答える
0

ボタンを相対的なレイアウトに配置します。ボタンにプロパティを追加 allignParentleft = true、別の allignParentcenter= true および allignParentRight = true を各ボタンに追加します。

于 2012-07-26T12:16:49.110 に答える
0

次のレイアウト スニペットを検討してください。

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="3">

    <ImageView
        android:src="@drawable/logo1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="left|center_vertical" />

    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Some Title"
        android:textColor="@android:color/black"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_weight="1" />

    <ImageView
        android:src="@drawable/logo2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="right|center_vertical" />

</LinearLayout>

以上2点注意。

A. weightSum が 3 の LinearLayout を作成しました。

B. 次に、その中に、layout_weight が 1 の 3 つの要素を作成して、子要素がスペース全体を均等に分散できるようにします。

于 2017-06-07T07:19:38.617 に答える