LinearLayout内で子要素の重みを指定する方法は何ですか?

LinearLayout内では、android:layout_weight属性を使用して、子要素が占める重みを指定できます。この属性の値は浮動小数点数で、親レイアウト内で子要素が占める割合を指定します。

例えば、LinearLayout内に子要素が2つある場合、1つはandroid:layout_weight=”1″が設定され、もう1つはandroid:layout_weight=”2″が設定されていると、最初の子要素は全体のスペースの1/3を占め、次の子要素は全体のスペースの2/3を占めます。

サンプルコードは次のようになります:

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

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Item 1" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="Item 2" />

</LinearLayout>

上記の例では、最初のTextViewの重みは1であり、2番目のTextViewの重みは2です。したがって、最初のTextViewは全体のスペースの1/3を占め、2番目のTextViewは全体のスペースの2/3を占めます。

bannerAds