※Android技術者認定にも役に立ちます!
LinearLayout
Layout内にViewを【縦】また【横】に並べて配置が可能なLayout。
LinearLayout重要属性
◉ android:orientation
レイアウトの方向を指定できる属性。※Defaultは【horizontal】
○ horizontal
→配置の方向を【横】基準にしてViewを配置できる。※左→右
↓Sampleコード
layout.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
○ vertical
→配置の方向を【横】基準にしてViewを配置できる。※上→下
↓Sampleコード
layout.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
◉ android:weightSum
画面を設定値した数分、分割りできる属性
↓Sampleコード
layout.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="4">
</LinearLayout>
◉ android:layout_weight
画面の設定値分、自分の範囲を親Layoutの余白(スペース)に拡大する。(埋めるイメージ)
↓Sampleコード
layout.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</LinearLayout>
※1は100%を示す。(設定範囲は0.1~1まで)
属性を組み合わせて使ってみよう!
◉横を基準にした場合。
↓Sampleコード
layout.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="4"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
◉縦を基準にした場合。
↓Sampleコード
layout.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="4"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
✳︎追加説明。
◉ match_parentとは
親に合わせるとの指示。
◉ wrap_contentとは
自分の子ビューに合わせるか、部品に合わせる。