新ブログ「ごんフラブログG2」

WordPressで始めてみました

[海外]バンドルされた複数のゲーム・PCアプリが購入できるサイト
humblebundle.com


ごんたろうのぼやきトップ

2017年07月12日

【Android開発】ScrollViewの使い方

ScrollViewの使い方がややこしいので
備忘録として。
実行エミュレータはGenymotion、
Nexus4(4.1.1:LV16)、Nexus5(5.0.0:LV21)
です。

要は
ヘッダー/スクロールビュー/フッター
と言った感じで、3つにブロックを分ける。
大元のレイアウトは
RelativeLayoutで
ヘッダーフッターはLinearLayoutで構成。

スクロールビューには
「layout_below(header)」「layout_above(footer)」を
指定する。ちなみに
layout_belowとlayout_aboveはRelativeLayoutの属性?

大元のRelativeLayoutは適当で良いので、
次からのLinearLayout〜
見てもらえば良いかと。

values.xmlなどを使ってないので
以下のレイアウトコード単体で問題ないはず…。
なので、MainActivity等と適当に作って
setContentViewで下記のコードを書いた
ファイルR.layout.(ここは任意)を指定して
やれば問題ないんじゃ無いかな?

テキストサイズやら、背景やら、
テキストビューの数は適当に
変えてもらって大丈夫です。
スクロールビューが正常な状態かを
確認するために必要以上のテキストビューを
並べているだけなので。

無駄な部分も多々ありますが、必要に応じて
削ったり足したりしてください。

footer部分が3行になってますが、
headerと構成を入れ替えたりも
出来ると思うので、こちらも
必要に応じて。

背景が黄色い「hoge〜」の部分が
スクロールビューの部分になります。

エミュレータ上ではこんな感じになります。
WS000693.JPG


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/header"
android:background="#FFEEFFFF"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="header"
android:textSize="50dp"
android:id="@+id/tv1"
android:layout_alignParentTop="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/footer"
android:background="#FFEEFFEE"
android:layout_alignParentBottom="true"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50dp"
android:id="@+id/footer_txt01"
android:text="footer01"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50dp"
android:id="@+id/footer_txt02"
android:text="footer02"
android:layout_below="@id/footer_txt01"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50dp"
android:id="@+id/footer_txt03"
android:text="footer03"
android:layout_below="@id/footer_txt02"
/>
</RelativeLayout>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/scroll_view"
android:layout_below="@+id/header"
android:layout_above="@id/footer"
android:background="#FFFFFFEE"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50dp"
android:text="hoge01"
android:id="@+id/hoge1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50dp"
android:text="hoge02"
android:id="@+id/hoge2"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50dp"
android:text="hoge03"
android:id="@+id/hoge3"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50dp"
android:text="hoge04"
android:id="@+id/hoge4"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50dp"
android:text="hoge05"
android:id="@+id/hoge5"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50dp"
android:text="hoge06"
android:id="@+id/hoge6"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50dp"
android:text="hoge07"
android:id="@+id/hoge7"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50dp"
android:text="hoge08"
android:id="@+id/hoge8"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50dp"
android:text="hoge09"
android:id="@+id/hoge9"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50dp"
android:text="hoge10"
android:id="@+id/hoge10"
/>
</LinearLayout>
</ScrollView>
</RelativeLayout>

posted by ごんたろう(Gontaro-Soft) at 20:25 | Comment(0) | Androidアプリ開発/Android Studio
この記事へのコメント
コメントを書く
お名前: [必須入力]

メールアドレス:

ホームページアドレス:

コメント: [必須入力]

認証コード: [必須入力]


※画像の中の文字を半角で入力してください。
ブログランキング・にほんブログ村へ
にほんブログ村

インターネット・コンピュータランキング