Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีใช้ ScrollView ใน Android

ภาพรวม

ตัวอย่างนี้สาธิตการใช้ ScrollView เพื่อให้เนื้อหาที่ยาวเกินหน้าจอสามารถเลื่อนดูได้ พร้อมปรับแต่ง Scrollbar

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

Android Studio → File → New Project

ขั้นตอนที่ 2: Layout (res/layout/activity_main.xml)

<?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 1" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 2" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 3" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 4" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 5" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 6" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 7" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 8" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 9" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 10" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 11" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 12" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 13" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 14" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 15" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 16" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 17" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 18" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 19" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 20" />

    </LinearLayout>
</ScrollView>

ขั้นตอนที่ 3: โค้ดหลัก (src/MainActivity.java)

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);
    }
}

ขั้นตอนที่ 4: Manifest

<?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>

การทดสอบ

รันแอปบนอุปกรณ์จริง - จะเห็นปุ่ม 20 ปุ่มสามารถเลื่อนขึ้น-ลงได้ พร้อม Scrollbar ที่ปรับขนาดแล้ว

เคล็ดลับเพิ่มเติม

  • android:fillViewport="true" - ทำให้ child ขยายเต็มพื้นที่เมื่อเนื้อหาน้อย
  • android:scrollbars="vertical|horizontal" - แสดง Scrollbar ทั้งแนวตั้งและแนวนอน
  • สำหรับรายการมาก ให้ใช้ RecyclerView แทน เพื่อประสิทธิภาพที่ดีกว่า
  • NestedScrollView ใช้เมื่อต้องการ Scroll ภายใน Scroll อีกชั้น