Computer >> 컴퓨터 >  >> 프로그래밍 >> Android

Kotlin으로 Android에서 스크롤 가능한 TextView 만드는 방법

이 튜토리얼에서는 Kotlin을 활용해 Android 앱에서 스크롤 가능한 TextView를 구현하는 방법을 단계별로 살펴봅니다. TextView에 긴 텍스트가 들어갈 때 화면 안에서 자유롭게 위아래로 스크롤할 수 있도록 만드는 것이 목표입니다.

1단계: 새 프로젝트 생성

Android Studio를 실행하고 File → New Project 메뉴로 이동한 뒤, 새 프로젝트 생성에 필요한 모든 정보를 입력하여 빈 프로젝트를 만듭니다.

2단계: 레이아웃 파일 작성 (res/layout/activity_main.xml)

activity_main.xml 파일에 아래 코드를 추가합니다. 핵심은 TextView에 android:scrollbars="vertical" 속성을 지정하는 것입니다.

<?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"
        android:textColor="@android:color/background_dark"
        android:textSize="48sp"
        android:textStyle="italic" />

</RelativeLayout>

3단계: MainActivity.kt 작성 (src/MainActivity.kt)

MainActivity.kt 파일에 아래 코드를 추가합니다. 여기서 가장 중요한 부분은 ScrollingMovementMethod()를 TextView의 movementMethod로 설정하는 것입니다. 이 메서드를 지정해야 실제로 스크롤 동작이 활성화됩니다.

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import android.widget.TextView

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"

        val textView: TextView = findViewById(R.id.textView)
        val text: String = "Your time is limited, so don’t waste it living someone else’s life. Don’t be trapped by dogma – which is living with the results of other people’s thinking. " + "If life were predictable it would cease to be life, and be without flavor. The big lesson in life, baby, is never be scared of anyone or anything."

        textView.text = text
        textView.movementMethod = ScrollingMovementMethod()
    }
}

4단계: AndroidManifest.xml 확인

androidManifest.xml 파일이 아래와 같이 작성되어 있는지 확인합니다.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.example.q11">

    <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) 아이콘을 클릭하세요. 실행 옵션에서 자신의 모바일 기기를 선택하면, 기기 화면에 스크롤 가능한 TextView가 표시되는 것을 확인할 수 있습니다.

Kotlin으로 Android에서 스크롤 가능한 TextView 만드는 방법