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アプリ:ボタンやEditTextに余白(パディング)を設定する

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

Androidアプリ:ストアの掲載情報

Androidアプリのプログラムが完成したらすぐにGoogle Playにアップロードできるわけではありません。 プログラム以外に必要なデータを以下にリストアップしました。 ストアの掲載情報として必要…続きを読む

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

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

Androidアプリ:ボタンを押すとメッセージが入れ替わる

Android StudioでAndroidアプリを試作します。 今回は「ボタンを1回押すとテキストの内容が変わり、もう1回押すと元に戻る」というだけのアプリをつくります。 プロジェクトの生成 And…続きを読む

Androidアプリ:App is not indexable by Google Search;と表示される

Android Studioで、 App is not indexable by Google Search; consider adding at least one Activity with a…続きを読む

Androidアプリ:フォーカスをあてたEditTextのテキストを全選択する

Android StudioでEditTextにフォーカスが当たったと同時に、既に入力されている文字列をすべて選択した状態にする方法です。 EditTextタグに以下のコードを追加 android:s…続きを読む

Androidアプリ:テキストをリスト表示する

Androidアプリで、ListViewを使い、配列に格納したテキストをリスト表示します。 MainActivity.java import android.app.Activity; import …続きを読む

Androidアプリ:AndroidXに対応する

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

Androidアプリ:画面の高さと幅を取得する

Andoroidで、画面の高さと幅を取得する方法です。 MainActivity.java Android端末の画面サイズ(高さと幅)を表示します。 public class QuestionActi…続きを読む

Androidアプリ:内部テスト版をリリースする

Androidアプリで完成したものをすぐに公開する前に、内部テスト版としてリリースして動作確認をする方法です。 内部テスト版リリース方法 Google Play Consoleにログインして、「すべて…続きを読む

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny