Android
 Computer >> コンピューター >  >> プログラミング >> Android

【Android】ScrollViewでスクロールバーを実装・カスタマイズする方法

はじめに

この記事では、AndroidアプリでScrollView(スクロールビュー)を使用して、画面の内容が収まらない場合でも上下にスクロールできるようにする方法を、実際のコード例とともにステップ形式で解説します。あわせて、android:scrollbarSize属性を使ってスクロールバーの太さを変更する方法も紹介します。

ステップ1:新規プロジェクトを作成する

まず、Android Studioを起動し、メニューから「File」→「New Project」を選択して新しいプロジェクトを作成します。必要な設定項目をすべて入力し、プロジェクトのセットアップを完了させてください。

ステップ2:activity_main.xml にコードを追加する

次に、res/layout/activity_main.xml に以下のコードを記述します。ルート要素としてScrollViewを配置し、その中に縦方向のLinearLayoutを入れてボタンを多数並べています。これにより、画面に収まりきらないコンテンツをスクロールして表示できるようになります。

ポイントは android:scrollbarSize="25sp" の指定です。この属性でスクロールバーの太さを自由に調整できます。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16sp"
    android:scrollbarSize="25sp"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="8dp"
        android:gravity="center_horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>
</ScrollView>

ステップ3:MainActivity.java にコードを追加する

続いて、src/MainActivity.java に以下のコードを記述します。今回はレイアウトの表示だけで動作を確認できるため、Activity側では特別な処理は不要です。作成したレイアウトファイルを読み込むだけでOKです。

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

補足:新しいバージョンのAndroid Studioでプロジェクトを作成した場合、サポートライブラリの代わりにAndroidXが使われます。その場合は import 文を androidx.appcompat.app.AppCompatActivity に置き換えてください。

ステップ4:AndroidManifest.xml を確認する

最後に、androidManifest.xml が以下のようになっていることを確認します。ScrollViewの使用に特別なパーミッションは必要ありません。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

アプリを実行して確認する

それでは、アプリを実際に実行してみましょう。実機のAndroidスマートフォンをパソコンに接続していることを前提としています。Android Studioでプロジェクト内のアクティビティファイルを開き、ツールバーの「Run」アイコンをクリックしてください。デバイス選択ダイアログが表示されたら、接続したモバイルデバイスを選択します。

アプリが起動すると、端末にはボタンが縦に並んだ画面が表示され、指でスワイプすることで画面を上下にスクロールできます。スクロール中には、scrollbarSizeで指定した太さのスクロールバーが画面右端に表示されます。

【Android】ScrollViewでスクロールバーを実装・カスタマイズする方法

まとめ

ScrollViewを使えば、画面サイズを超えるコンテンツでも簡単にスクロール表示できます。レイアウトXMLにScrollViewを配置し、android:scrollbarSizeなどの属性でスクロールバーの見た目を調整するだけで、直感的に操作できるUIが実装できます。ぜひ自分のアプリにも取り入れてみてください。

  1. Android SQLiteでunlikely()関数を使用する方法をわかりやすく解説

    AndroidにおけるSQLiteデータベースとは 実装例に入る前に、まずAndroidにおけるSQLiteデータベースの基本を押さえておきましょう。SQLiteはオープンソースのSQLデータベースエンジンで、デバイス上のテキストファイルとしてデータを保存するのが特徴です。 Androidには標準でSQLiteデータベースの実装が組み込まれており、リレーショナルデータベースが持つすべての機能をサポートしています。また、JDBCやODBCのような接続設定を行う必要がなく、そのまま手軽にデータベースへアクセスできる点も大きなメリットです。 本記事では、Android SQLiteでunlikely

  2. 【Android】SharedPreferencesの使い方をサンプルコード付きでわかりやすく解説

    AndroidのSharedPreferencesとは? SharedPreferencesは、Androidアプリで少量のデータを「キーと値(Key-Value)」のペアとして永続的に保存するための標準的な仕組みです。保存されたデータはアプリ内部のファイルに書き込まれるため、アプリを終了して再起動した後でも値が保持されます。ユーザー名やパスワード、各種設定情報の保存などによく利用されます。 この記事では、ログイン画面を例に「名前・パスワード・チェックボックスの状態を保存し、次回起動時に復元する」サンプルを通じて、SharedPreferencesの基本的な使い方をステップごとに解説します。