HOME技術AndroidAndroidアプリ:レイアウトの途中にスクロールを入れる

Androidアプリ:レイアウトの途中にスクロールを入れる

Androidアプリで以下のようなレイアウトを組む方法です。

weightSumを使う

上、中央、下のパーツに分けて、上下のパーツは固定、中央のパーツはスクロール可能にします。
最初にLinearLayoutをverticalで大外の枠をつくります。
それから上段と下段はLinearLayout、中央はScrollViewにします。
これらを比率で分割するには、親要素のLinearLayoutに
android:weightSum=”1″を設定します。
これにより、全体で1になるように比率を組むことができるようになります。
ここでは、最初のLinearLayoutはandoroid:layout_weight=”0.2″
中央のScrollViewはandroid:layout_weight=”0.7、
最後のLinearLayoutはandroid:layout_weight=”0.1″
のように設定してあります。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:weightSum="1"
    >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:orientation="horizontal"
    android:weightSum="1"
    android:layout_weight="0.2"
    >
</LinearLayout>
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.7"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:weightSum="1"
        >

    <Button
        android:id="@+id/q1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="------"
        android:textSize="30sp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        />

    <Button
        android:id="@+id/q2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="------"
        android:textSize="30sp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        />
    </LinearLayout>

</ScrollView>
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="0.1"
        android:weightSum="1"
        android:gravity="right"
        >

        <Button
            android:id="@+id/help"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_weight="0.2"
            android:layout_marginRight="10dp"
            android:text="help"
            />

</LinearLayout>
</LinearLayout>
</LinearLayout>

関連記事

Androidアプリ:AndroidXに対応する

2018年に、AndroidのSupportLibraryは、AndroidXに移行することが発表されました。 AndroidXとは Support Libraryとは簡単に言うと、Androidアプ…続きを読む

Androidアプリ:特定のViewに最初のフォーカスをあてる

Android Studioにおいて、起動時に特定のViewにフォーカスをあてる方法です。 activity_main.xmlで設定 レイアウト定義ファイル(activity_main.xml)におい…続きを読む

Androidアプリ:画面をタップしたら文字を入れ替える

画面をタップしたら文字を入れ替えるアプリをつくります。 今回はタッチイベントを取得する方法を調べました。 ここではレイアウト定義ファイルを「activity_main.xml」、 プログラムファイルを…続きを読む

Androidアプリ:ボタンやEditTextを角丸にする

Android StudioでボタンやEditTextを配置して、角丸にします。 角丸や色などを指定する場合には、それ専用のxmlファイルを別途用意し、読み込ませるようにします。 ボタンの背景色と角丸…続きを読む

Google広告でポリシー違反「句読点と記号」に対応する

モバイルアプリのGoogle広告のキャンペーンを設定しましたが、「ポリシー違反1件」と表示されました。 ポリシー違反「句読点と記号」 ポリシー違反の詳細を見ると、「句読点と記号」というヒントが表示され…続きを読む

Androidアプリ:エラー「ScrollView can host only one direct child」

Android StudioでScrollViewを設定して動作確認しようとすると、「ScrollView can host only one direct child」というエラーが出て動作確認でき…続きを読む

Androidアプリ:When using intent filters, please specify ‘android:exported’ as well

Android Studioで「When using intent filters, please specify ‘android:exported’ as well」という…続きを読む

Androidアプリ:ボタンやEditTextに余白(パディング)を設定する

Android StudioでボタンやEditTextに余白(パディング)を設定します。 余白の設定はレイアウトファイル(activity_main.xml)を編集します。 activity_main…続きを読む

Androidアプリ:String型の変数に格納した画像や音声ファイル名から表示・再生する

Android Studioを使って画像ファイル名や音声ファイル名から、ImageViewで画像を表示したりMediaPlayerで音声再生する方法です。 String型の変数に、画像ファイル名もしく…続きを読む

Androidアプリを実機で確認する

AndroidアプリをAndroid Studioで開発した際に、実際のスマホの画面で動作確認する方法です。 スマホの設定 以下はArrows FX F-02Hの設定方法です(開発者向けオプションを表…続きを読む