Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách tạo TextView có thanh cuộn (scrollable) trong Android

Giới thiệu

Hướng dẫn này trình bày cách tạo TextView có khả năng cuộn (scrollable) để hiển thị văn bản dài trong Android.

Các bước thực hiện

Bước 1: Tạo dự án mới

Mở Android Studio, chọn File → New Project và điền thông tin cần thiết.

Bước 2: Thiết kế giao diện (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />
</RelativeLayout>

Bước 3: Viết logic xử lý (src/MainActivity.java)

package app.com.sample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        TextView textView = findViewById(R.id.textView);
        
        // Văn bản dài mẫu (tiểu sử Mahatma Gandhi)
        String longText = "Mahatma Gandhi (1869-1948) là nhà lãnh đạo vận động độc lập Ấn Độ...";
        // (Văn bản đầy đủ khá dài, đã rút gọn cho bài viết)
        
        textView.setText(longText);
        
        // QUAN TRỌNG: Bật chế độ cuộn
        textView.setMovementMethod(new ScrollingMovementMethod());
    }
}

Bước 4: Cấu hình Manifest (androidManifest.xml)

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

Giải thích các thành phần quan trọng

1. XML: android:scrollbars="vertical"

Hiển thị thanh cuộn dọc. Có thể dùng horizontal hoặc vertical|horizontal.

2. Java: setMovementMethod(new ScrollingMovementMethod())

Đây là bước bắt buộc để TextView thực sự có thể cuộn được. Nếu thiếu dòng này, thanh cuộn hiển thị nhưng không tương tác được.

Các tùy chọn bổ sung

// Tự động cuộn xuống dưới cùng
textView.post(() -> textView.scrollTo(0, textView.getBottom()));

// Giới hạn số dòng hiển thị (cần scroll để xem thêm)
textView.setMaxLines(10);
textView.setEllipsize(TextUtils.TruncateAt.END);

// Padding để text không dính mép
textView.setPadding(16, 16, 16, 16);

// Kích thước chữ
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);

Chạy ứng dụng

Kết nối thiết bị Android, nhấn Run trong Android Studio. Văn bản dài sẽ hiển thị với thanh cuộn dọc, cho phép vuốt lên/xuống để đọc toàn bộ nội dung.

Cách tạo TextView có thanh cuộn (scrollable) trong Android