安卓之文本视图TextView及跑马灯效果

一、基本属性和设置方法

二、跑马灯用到的属性与方法说明

三、省略方式的取值说明

四、跑马灯效果案例代码

   (1)布局xml文件 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- 普通文本视图 -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="跑马灯效果" />

    <!-- 跑马灯滚动文本视图 -->
    <TextView
        android:id="@+id/tv_marquee"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:textColor="#000000"
        android:textSize="20sp"
        android:text="快讯:床前明月光疑是地上霜巨头望明月低头思故乡" />
</LinearLayout>

   (2)java代码

package com.example.horselantern;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_marquee;
    private boolean bPause = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_marquee = findViewById(R.id.tv_marquee);
        tv_marquee.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_marquee) {
            bPause = !bPause;
            if (bPause) {
                tv_marquee.setFocusable(false);
                tv_marquee.setFocusableInTouchMode(false);
            } else {
                tv_marquee.setFocusable(true);
                tv_marquee.setFocusableInTouchMode(true);
            }
        }
    }
}
原文地址:https://www.cnblogs.com/soldierback/p/10808204.html