内容はどう違うの?という話ですが、ほぼ同じです。
なので、今回はサラッと流します。
注目すべき点は、同じくlayout_weightです。
では、サンプルを見てみましょう。
サンプルアプリで、メニューを押して、chapter04を選択してください。
以下のような画面になります。
この画面は、LinearLayoutで横方向配置を指定した後に、さらにLinearLayoutを3つ入れて、それぞれは縦方向配置の設定をしています。
layout_weightは、レイアウトの重みづけで、高ければ高いほど重要度が増すとChapter03で説明しました。このレイアウトも、Chapter03の縦版なので、同様の設定になっています。
コードレビュー
では、レイアウトのソースコードを見てみましょう。Sample01プロジェクトの/res/layout/chapter04.xmlを開いてください。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!--
縦方向均等配置をするため、親要素のLinearLayoutのorientationをverticalに設定します。
ここでは、layout_weightが肝です。指定された方向に対してウェイトが重い分だけ、拡がります。
デフォルトは0なので、1つの要素でも1を与えるとそれが残りの領域を占拠します。
また、均等配置する方向が横の場合、layout_widthに有効な値をいれても無駄になるので0dpを入れます。
-->
<!-- 均等配置
全ての要素にlayout_weight="1"を設定 -->
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="@string/chapter04_button1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="@string/chapter04_button2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="@string/chapter04_button3" />
</LinearLayout>
<!-- 1つの要素だけlayout_weightを設定
ただし、layout_weightが設定されてないボタンが表示されるように、
layout_height="wrap_content"にしてます。 -->
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chapter04_button4" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chapter04_button5" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="@string/chapter04_button6" />
</LinearLayout>
<!-- 全ての要素にlayout_weightを設定
但し、button9のみweightを2にしている -->
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="@string/chapter04_button7" />
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="@string/chapter04_button8" />
<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="2"
android:text="@string/chapter04_button9" />
</LinearLayout>
</LinearLayout>
※以降、Chapter03と同じ言い回しになりますので、無視したい人は無視してください。注目するべきは、layout_heightの値を0dpにしているところです。 layout_heightに0dp以外の有効な値を入れると、無駄に解析が入ってしまって非効率だからです。モバイル端末は基本的に非力だしバッテリー持たせる必要があるので、無駄な処理は極力削ります。それはプログラムだけでなく、レイアウトのXMLでも言えるのです。
layout_heightの効能は、LinearLayoutのorientationの方向で意味づけが決まるので、verticalの場合は、縦方向に効きます。なので、layout_height="0dp"なわけです。
均等配置がlayout_weightを指定するだけで簡単にできることがわかってもらえたかと思います。


0 件のコメント:
コメントを投稿