LinearLayoutの幅全体にボタンを均等に配置することは可能ですか?質問する

LinearLayoutの幅全体にボタンを均等に配置することは可能ですか?質問する

3 つのボタンを含む (水平方向)がありますLinearLayout。3 つのボタンの幅を固定し、 の幅全体に均等に分散させたいと思いますLinearLayout

の重力をLinearLayout中央に設定し、ボタンのパディングを調整することでこれを管理できますが、これは固定幅に対して機能し、異なるデバイスや方向では機能しません。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal">

    <Button
        android:id="@+id/btnOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="120dp" />

    <Button
        android:id="@+id/btnTwo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="120dp" />


    <Button
        android:id="@+id/btnThree"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="120dp" />
</LinearLayout>

ベストアンサー1

拡大するfedjの回答layout_widthを に設定し0dp、各ボタンの を 1 に設定するとlayout_weight、使用可能な幅がボタン間で均等に共有されます。

おすすめ記事