單個跑馬燈效果其實只需要把TextView的屬性設置成下面即可:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.marqueetextview.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="@string/text_view" />
</LinearLayout>
但是這個跑馬燈是有缺點的,只能存在一個跑馬燈,你再出現幾個TextView都不會有跑馬燈的效果,只能第一個TextView具有跑馬燈效果。下面就給而出解決辦法:
寫一個類繼承與TextView,重寫它的構造方法和isFocused()方法。只需要把isFocused()方法返回值改成true即可。下面是代碼:
package com.example.marqueetextview;
import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
/**
* Created by lxjhoney on 2017/8/21.
*/
public class MarqueeTextView extends AppCompatTextView {
public MarqueeTextView(Context context) {
super(context);
}
public MarqueeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MarqueeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean isFocused() {
// 把返回值改成true即可
return true;
}
}
現在就只需要把這個MarqueeTextView用到佈局裏面去即可,下面是xml代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.marqueetextview.MainActivity">
<com.example.marqueetextview.MarqueeTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="@string/text_view" />
<com.example.marqueetextview.MarqueeTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="@string/text_view" />
<com.example.marqueetextview.MarqueeTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="@string/text_view" />
<com.example.marqueetextview.MarqueeTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="@string/text_view" />
<com.example.marqueetextview.MarqueeTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="@string/text_view" />
</LinearLayout>
這樣就多少個TextView都能跑馬燈了。這裏就不上圖了