ウェブアプリケーション,インジェクション,コマンドインジェクション

Androidアプリ開発 Viewをつかったインターフェースデザイン

Viewの位置取りのためにLineaLayoutを使う

Using LinearLayout for View Positioning

アクティビティはViewとViewグループを持っています。
Viewはスクリーンに現れているウィジェットで、
Viewの例は、ボタン・テキストview・エディットテキストなどです。
Viewはandroud.view.Viewの基底クラスから引き出されます。
1つまたは、複数のviewをViewGroupとしてグループ化させられます。
グループ自体がviewの特別な型となります。
ViewGroupはviewの順序と体裁を整えられるレイアウトを提供します。
ViewGroupの例は、LinearLayoutとFrameLayoutが含まれます。
ViewGroupはandroid.view.ViewGroup基底クラスから引き出されます。
AndroidがサポートしているViewGroupは、
●LinearLayout
●TabletLayout
●RelativeLayout
●FrameLayout
●ScrollView
欲しいUIを作るために異なるレイアウトを合わせていくのが
一般的であることに注意します。
以下のコードはLinearLayoutのViewGroupを示します。 LinearLayoutを使ってViewを整えるときにはいくつかの方法があります。
●1つの行や1つの列に整えることができる。
●Child Viewを縦や横のどちらかに整えさせることができる。
●Viewはweight使用を使って、スクリーンサイドに確実に整列させられる。
続くセクションでもう少し説明します。

orientations

orientationを横や縦に表示するためにセットできます。
すると、アイテムは1つの列や1つの行に現れます。
そこで、以下のようなコード部がactivity_main.xmlファイルに
あるとします。

<LinearLayout xmlns:android=""http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="horizontal" >

   <Button
     android:layout_width="100dp"
     android:layout_height="wrap_content"
     android:text="Button 1" />

   <Button
     android:layout_width="100dp"
     android:layout_height="wrap_content"
     android:text="Button 2" />

   <Button
     android:layout_width="100dp"
     android:layout_height="wrap_content"
     android:text="Button 3" />
<LinearLayout>

上のコードはボタンを横並びに3つ用意します。
特徴はボタンが左から右へ並んでいくことに注目してください。
これはLinearLayoutのorientationにhorizontalをセットしたためです。
もしLinearLayoutのorientationにverticalをセットすると、
上から順番にボタンが配置されます。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" >

Gravity and weight

横や縦にviewを並べることに加え、ViewのGravityとWeightを
指定することもできます。
ViewのGravityはコンテナ内にどのように配置させるべきかを示します。
(左、右、中央に整列します。)
ViewのWeightは同じコンテナの他のViewに配慮して、
そのViewの配置を指定します。
(他のアイテムの上、下に配置させることができます。)
GravityとWeightの指定は次のコードの様にします。

<Button
   android:layout_width="100dp"
   android:layout_height="wrap_content"
   android:layout_grabity="left"
   android:layout_weight="1"
   android:text="Button 1" />

<Button
   android:layout_width="100dp"
   android:layout_height="wrap_content"
   android:layout_gravity="center"
   android:layout_weight="2"

   android:text="Button 2" />

<Button
   android:layout_width="100dp"
   android:layout_height="wrap_content"
   android:layout_gravity="right"
   android:layout_weight="3"

   android:text="Button 3" />

layout_gravity属性はコンテンツのポジションを示します。
layout_weight属性は利用できるスペースの分布状態を指定します。
上のコードではweight="1"にしたら、1/(1+2+3)×100で
画面上下の割合を計算し、weight="1"は16.6%になり、
同じく、weight="2"では2/(1+2+3)×100なので、33.3%。
weight="3"では3/(1+2+3)×100で、50%の高さになります。
このactivity_main.xmlファイルにさらにもう1つのボタンを
加えた場合、weight="3"に指定したときのボタンの高さは、
3/(1+2+3+4)×100の計算になり、30%の高さになります。

ホーム
便利堂ロゴ
inserted by FC2 system