Amazon banner

2010年10月27日水曜日

Android layout.xml include RelativeLayout

たまにはまともな話。
Androidのlayout.xmlはJSPのようにincludeする事が出来る。
こんな感じ。

■main.xml
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android">
    <include layout="@layout/include" />
</LinearLayout>

■include.xml その1
Layoutを使いたくない場合はmergeタグを使う。
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <imageview ~ />
    <textview ~ />
</merge>

■include.xml その2
Layoutを保持しておきたい場合は、そのままでOK
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android">
    <imageview ~ />
    <textview ~ />
</LinearLayout>


LinearLayoutの場合はいいのだが、RelativeLayoutを使いたい場合もある。
その場合、
    <include layout="@layout/include" android:layout_alignParentBottom="true" />
と書いてもエラーにはならないのだが、反映されない。
で、どうするかというと、
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true">
    <include layout="@layout/include" />
</LinearLayout>

と適当なLayoutでくくっちゃえばいいのだ。

覚えておこう。

0 件のコメント:

コメントを投稿