レイアウトエディタを使う

Eclipseの左側にあるPackage ExplorerからProject→res→layoutとたどると、
main.xmlがある。デフォルトの中身はこんな感じ。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
</LinearLayout>

この場合、画面上部にHello world的な文字列が出力される。
XMLでの編集も出来るけど、GUIによる見た目でのボタン配置にも対応している。
Graphical Layoutタブに切り替えれば、画面の配置をマウスで決定できる。

Graphical Layoutで設定可能な設定としては、Widgetの配置のほか、
画面の上部には以下のような設定が並んでいる。

  • 画面解像度(QVGAなど)
  • 画面の向き(Portrait=縦、またはLandscape=横)
  • 画面のテーマ(タイトルバーを隠す、白基調にする、など)
  • Androidのバージョン

作成したレイアウトは以下のようにして組み込める。ここではmybuttons.xmlを作成したものとする。
(大文字がファイル名に入ってはいけないらしい)

View myButtons = View.inflate(this, R.layout.mybuttons, null);
addContentView(myButtons, new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));