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

Thêm xuống dòng (Line Break) trong TextView Android Kotlin

Hướng dẫn này minh họa cách thêm xuống dòng (line break) trong TextView trên Android sử dụng Kotlin.

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 (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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="Tutorials Point"
        android:textAlignment="center"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="32sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/Questions"
        android:textSize="24sp" />

</RelativeLayout>

Bước 3: Viết code MainActivity.kt

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
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)
        textView.setTextColor(Color.RED)
    }
}

Bước 4: Thêm string resource với xuống dòng (res/values/strings.xml)

<resources>
    <string name="app_name">LineBreakDemo</string>
    <string name="Questions">
1. What is your favourite food?\n
2. Which is your favourite sport?\n
3. What is your bucket list?
    </string>
</resources>

Chạy ứng dụng

Kết nối thiết bị, nhấn Run. TextView hiển thị 3 câu hỏi trên 3 dòng riêng biệt, màu đỏ.

Các cách tạo xuống dòng trong Android

CáchCú phápÁp dụng cho
String resource\n trong strings.xmlXML layout, setText()
Kotlin code"Dòng 1\nDòng 2"setText() động
HTMLHtml.fromHtml("Dòng 1
Dòng 2")
Rich text, hỗ trợ tag HTML
XML layoutandroid:text="Dòng 1\nDòng 2"Static text

Ví dụ HTML (hỗ trợ style)

val htmlText = "Dòng 1
Dòng 2 in đậm
Dòng 3 nghiêng" textView.text = Html.fromHtml(htmlText, Html.FROM_HTML_MODE_LEGACY)

Kotlin Multi-line String (Raw String)

val multiLine = """
    Dòng 1
    Dòng 2
    Dòng 3
""".trimIndent()

textView.text = multiLine

XML Entity (trong strings.xml)

<string name="multiline">Dòng 1
Dòng 2
Dòng 3</string>
<!-- hoặc -->
<string name="multiline">Dòng 1
Dòng 2
Dòng 3</string>

Lưu ý quan trọng

  • \n trong strings.xml hoạt động tốt nhất
  • Trong Kotlin code: dùng "\n" (double quotes), KHÔNG dùng '\n' (single quotes)
  • Html.fromHtml() cần import android.text.Html
  • TextView phải đủ chiều cao (wrap_content hoặc maxLines lớn)