[Android] ScrollViewのfillViewportの解説

| トラックバック(0) |

Android Developer Blog によると、ScrollView の fillViewport の解説が、

ROMAIN GUY さんのブログで解説されていたようです。

http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/

 

参照元のブログによると、

スクロールバーを使って、画面の高さより大きいレイアウトを表示させるには、

スクロールさせたいビューの親に「ScrollView」に配置します。

 

これにより、下図のように、配置したレイアウトの高さが、画面の高さより大きい場合、

スクロールバーが表示され、(スクロールさせることにより)レイアウト全体を表示させることができます。

※この結果、一番下のスクロール位置では、最下段に配置したボタンが画面下部に配置されます

ok_textview_long_top.png ok_textview_long_bottom.png

 

一方、下図のように、配置したレイアウトの高さが、画面の高さより小さい場合には、

「ScrollView」の高さ設定が、android:layout_height="fill_parent" であっても、

「ScrollView」の高さは、親ビューであるアクティビティーにフィットされず、子ビューの高さで表示されてしまいます。

ng_textview_short.png

 

上記のような表示を意図している場合には、このままで良いのですが、

下図のように、「ScrollView」の高さを、親ビューであるアクティビティーにフィットさせ、

レイアウト最下段を、画面下に配置させたい場合には、

「ScrollView」の高さ設定を、android:fillViewport="true" とする必要があるようです。

ok_textview_short.png

 

※ちなみに、この fillViewport 属性については、これまでドキュメント化されていなかったそうです^^;

 

ソースコード(原文より引用)

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroller"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="6dip"
            android:paddingRight="6dip"
            android:paddingTop="6dip"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Welcome to My Application" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="1dip"
            android:background="#ff106510"
            android:layout_marginLeft="6dip"
            android:layout_marginRight="6dip"
            android:layout_marginTop="6dip"
            android:layout_marginBottom="12dip" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"

            android:paddingLeft="6dip"
            android:paddingRight="6dip"
            android:paddingBottom="6dip"

            android:text="@string/hello" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"

            android:background="@android:drawable/bottom_bar"
            android:gravity="center_vertical">
            <Button
                android:layout_width="0dip"
                android:layout_weight="1.0"
                android:layout_height="wrap_content" 

                android:text="Accept" />
            <Button
                android:layout_width="0dip"
                android:layout_weight="1.0"
                android:layout_height="wrap_content" 

                android:text="Refuse" />
        </LinearLayout>
    </LinearLayout>
</ScrollView>

関連記事

トラックバック(0)

トラックバックURL: http://mt.adakoda.com/mt-tb.cgi/483

Android Advent Calendar 2011

2012年2月

      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29