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

Cách sử dụng văn bản chạy (Marquee) trong TextView Android

Giới thiệu

Ví dụ này minh họa cách tạo hiệu ứng văn bản chạy (marquee) trong TextView khi nội dung dài hơn chiều rộng hiển thị.

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 với TextView marquee (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:padding="4dp"
        android:id="@+id/textMarquee"
        android:textSize="24sp"
        android:textStyle="bold|italic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"/>
</RelativeLayout>

Các thuộc tính quan trọng:

  • android:singleLine="true" - buộc văn bản trên một dòng
  • android:ellipsize="marquee" - bật hiệu ứng marquee
  • android:marqueeRepeatLimit="marquee_forever" - lặp vô hạn
  • android:scrollHorizontally="true" - cho phép cuộn ngang

Bước 3: Kích hoạt marquee (MainActivity.java)

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

public class MainActivity extends AppCompatActivity {
    TextView textMarquee;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        textMarquee = findViewById(R.id.textMarquee);
        textMarquee.setText("Be the change that you wish to see in the world, Be yourself; everyone else is already taken.");
        textMarquee.setSelected(true); // Quan trọng: phải setSelected(true) để marquee hoạt động
    }
}

Bước 4: 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. Văn bản sẽ chạy liên tục nếu dài hơn chiều rộng TextView.

Cách sử dụng văn bản chạy (Marquee) trong TextView Android