タブレット向け開発

ちょっと思い立ったのでタブレットスマホの共通アプリの作り方を調べてみた。
タブレット(HC以降)のSDKで追加されたFragmentを使うというのは知ってたので。

レイアウト

ひとまずタブレットは実機のAuroraをベースに考える。
HC3.2からレイアウトの指定方法が追加されたらしい。
Auroraは1024x600の7インチなので「layout-sw600dp」を指定すれば良いらしい。

/layout/main.xmlはとりあえずスマホ用。
/layout-sw600dp/main.xmlはタブ用。

フラグメント用レイアウト

タブでよくある2ペインにするので、それようにレイアウトを作成。
/layout/fragment1.xml
/layout/fragment2.xml

Fragmentの作成

public class Fragment1 extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bandle savedInstanceState) {
        return inflater.inflate(R.layout.fragment1, container, false);
    }
}

それぞれレイアウトを関連づけたFragmentクラスをふたつ作成。

レイアウトにフラグメントを設定

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <fragment
        android:name="Fragment1"
        android:id="@+id/search"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content" />
    <fragment
        android:name="Fragment2"
        android:id="@+id/result"
        android:layout_width="0dp"
        android:layout_weight="3"
        android:layout_height="wrap_content" />
</LinearLayout>

width="0dp"にしてweight="1"とweightを指定すると画面幅に合わせた比率になるらしい。
↑この場合、1:3。


ソースコードここがわかりやすかった。