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

Cách ngắt dòng (line break) trong TextView Android

Giới thiệu

Ví dụ này minh họa các cách tạo ngắt dòng trong TextView: sử dụng ký tự \n trong string resource hoặc code.

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: 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_centerInParent="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:text="@string/Questions" />
</RelativeLayout>

Bước 3: Code đơn giản (MainActivity.java)

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        textView = findViewById(R.id.textView);
        textView.setTextColor(Color.RED);
    }
}

Bước 4: String resource với ngắt dòng (strings.xml)

<resources>
    <string name="app_name">Sample</string>
    <string name="Questions">
        1. Món ăn yêu thích của bạn là gì?\n
        2. Môn thể thao yêu thích?\n
        3. Danh sách điều muốn làm trong đời?
    </string>
</resources>

Giải thích: Trong file strings.xml, dùng \n để tạo dòng mới. Thẻ <br> không hoạt động trong string resource (chỉ dùng cho HTML).

Bước 5: 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>

Chạy ứng dụng

Kết nối thiết bị, nhấn Run. TextView hiển thị 3 dòng riêng biệt.

Cách ngắt dòng (line break) trong TextView Android

Các cách tạo ngắt dòng:
  • Trong strings.xml: \n
  • Trong code Java: textView.setText("Dòng 1\nDòng 2")
  • Html.fromHtml("Dòng 1
    Dòng 2") - cần API 24+ dùng FROM_HTML_MODE_LEGACY
  • XML attribute: android:text="Dòng 1\nDòng 2"