이 예제는 Kotlin을 사용하여 Android에서 페이지 매김 텍스트를 만드는 방법을 보여줍니다.
1단계 − Android Studio에서 새 프로젝트를 생성하고 파일 ⇒ 새 프로젝트로 이동하여 필요한 모든 세부 정보를 입력하여 새 프로젝트를 생성합니다.
2단계 − res/layout/activity_main.xml에 다음 코드를 추가합니다.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="16dp" android:paddingTop="16dp" android:paddingRight="16dp" android:paddingBottom="16dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/buttonBack" style="?android:attr/buttonBarButtonStyle" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@android:color/transparent" /> <Button android:id="@+id/buttonForward" style="?android:attr/buttonBarButtonStyle" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@android:color/transparent" /> </LinearLayout> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
3단계 − src/MainActivity.kt에 다음 코드 추가
import android.graphics.Color import android.graphics.Typeface import android.os.Bundle import android.text.Html import android.text.Spannable import android.text.SpannableString import android.text.TextUtils import android.text.style.ForegroundColorSpan import android.text.style.RelativeSizeSpan import android.text.style.StyleSpan import android.view.ViewTreeObserver import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { lateinit var buttonBack: Button lateinit var buttonForward: Button private lateinit var textView: TextView private var pagination: Pagination? = null private lateinit var charSequence: CharSequence private var currentIndex = 0 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "KotlinApp" buttonBack = findViewById(R.id.buttonBack) buttonForward = findViewById(R.id.buttonForward) textView = findViewById(R.id.tv) val htmlString = Html.fromHtml(getString(R.string.html_string)) val spanString = SpannableString(getString(R.string.long_string)) spanString.setSpan(ForegroundColorSpan(Color.BLUE), 0, 24, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) spanString.setSpan(RelativeSizeSpan(2f), 0, 24, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) spanString.setSpan(StyleSpan(Typeface.MONOSPACE.style), 0, 24, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) spanString.setSpan(ForegroundColorSpan(Color.BLUE), 536, spanString.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) spanString.setSpan(RelativeSizeSpan(2f), 536, spanString.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) spanString.setSpan(StyleSpan(Typeface.MONOSPACE.style), 536, spanString.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) charSequence = TextUtils.concat(htmlString, spanString) textView.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener { override fun onGlobalLayout() { // Removing layout listener to avoid multiple calls textView.viewTreeObserver.removeOnGlobalLayoutListener(this) pagination = Pagination( charSequence, textView.width, textView.height, textView.paint, textView.lineSpacingMultiplier, textView.lineSpacingExtra, textView.includeFontPadding ) update() } }) buttonBack.setOnClickListener { currentIndex = if ((currentIndex > 0)) currentIndex - 1 else 0 update() } buttonForward.setOnClickListener { currentIndex = if ((currentIndex <pagination!!.size() - 1)) currentIndex + 1 else pagination!!.size() - 1 update() } } private fun update() { val text = pagination!![currentIndex] if (text != null) textView.text = text } }
4단계 − Kotlin 클래스를 만들고 다음 코드를 추가합니다. −
import android.graphics.Color import android.graphics.Typeface import android.os.Bundle import android.text.Html import android.text.Spannable import android.text.SpannableString import android.text.TextUtils import android.text.style.ForegroundColorSpan import android.text.style.RelativeSizeSpan import android.text.style.StyleSpan import android.view.ViewTreeObserver import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { lateinit var buttonBack: Button lateinit var buttonForward: Button private lateinit var textView: TextView private var pagination: Pagination? = null private lateinit var charSequence: CharSequence private var currentIndex = 0 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "KotlinApp" buttonBack = findViewById(R.id.buttonBack) buttonForward = findViewById(R.id.buttonForward) textView = findViewById(R.id.tv) val htmlString = Html.fromHtml(getString(R.string.html_string)) val spanString = SpannableString(getString(R.string.long_string)) spanString.setSpan(ForegroundColorSpan(Color.BLUE), 0, 24, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) spanString.setSpan(RelativeSizeSpan(2f), 0, 24, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) spanString.setSpan(StyleSpan(Typeface.MONOSPACE.style), 0, 24, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) spanString.setSpan(ForegroundColorSpan(Color.BLUE), 536, spanString.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) spanString.setSpan(RelativeSizeSpan(2f), 536, spanString.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) spanString.setSpan(StyleSpan(Typeface.MONOSPACE.style), 536, spanString.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) charSequence = TextUtils.concat(htmlString, spanString) textView.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener { override fun onGlobalLayout() { // Removing layout listener to avoid multiple calls textView.viewTreeObserver.removeOnGlobalLayoutListener(this) pagination = Pagination( charSequence, textView.width, textView.height, textView.paint, textView.lineSpacingMultiplier, textView.lineSpacingExtra, textView.includeFontPadding ) update() } }) buttonBack.setOnClickListener { currentIndex = if ((currentIndex > 0)) currentIndex - 1 else 0 update() } buttonForward.setOnClickListener { currentIndex = if ((currentIndex < pagination!!.size() - 1)) currentIndex + 1 else pagination!!.size() - 1 update() } } private fun update() { val text = pagination!![currentIndex] if (text != null) textView.text = text } }
5단계 − 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 스튜디오에서 앱을 실행하려면 프로젝트의 활동 파일 중 하나를 열고 실행을 클릭합니다. 툴바에서 아이콘. 모바일 장치를 옵션으로 선택한 다음 기본 화면을 표시할 모바일 장치를 확인하십시오.