社内se × プログラマ × ビッグデータ

プログラミングなどITに興味があります。

LinearLayout を入れ子に配置 (Kotlin で Android アプリ開発)

LinearLayout は横方向か縦方向にビューを並べて表示させるレイアウト部品。
横方向と縦方向を混在させてビューを並べたい場合、LinearLayout を入れ子に配置する。

string.xml に追記

string.xml
以下の2行を追加する。

....
<string name="cb_1">Check1</string>
<string name="cb_2">Check2</string>

レイアウトファイルの編集

activity_main.xml
以下のように、既存の LinearLayout 内に LinearLayout を追加する。
追加した LinearLayout には、android:orientation="horizontal" を指定する。

<LinearLayout
    ....
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#df7400"
        android:orientation="horizontal">
        
        <CheckBox 
            android:id="@+id/cb1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:background="#ffffff"
            android:text="@string/cb_1"/>

        <CheckBox
            android:id="@+id/cb2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:background="#ffffff"
            android:text="@string/cb_2"/>
    </LinearLayout>
....
</LinearLayout>

アプリケーションを起動すると、チェックボックス部品が横並びになっていることが分かる。

f:id:blueskyarea:20200412122147p:plain
linarlayout