1. 程式人生 > >TextView水平輪播(流水燈效果)

TextView水平輪播(流水燈效果)

Layout的效果:

      <view.HomeTextView
          android:id="@+id/tv_home_marquee"
          android:singleLine="true"
          android:ellipsize="marquee"
          android:focusableInTouchMode="true"
          android:marqueeRepeatLimit="marquee_forever"
          android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="需要輪播的文字" android:textColor="#faf7f7" android:textSize="13sp" />

自定義的View:

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

public class HomeTextView
extends TextView {
//在程式碼中使用的呼叫 public HomeTextView(Context context) { super(context); } //在佈局檔案中使用的時候呼叫,比兩個引數多了樣式 public HomeTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } //在佈局檔案中使用的時候呼叫 //佈局檔案中的控制元件都是可以用程式碼來表示
//AttributeSet : 儲存了控制元件在佈局檔案中的所有屬性 public HomeTextView(Context context, AttributeSet attrs) { super(context, attrs); } //設定自定義的textview自動獲取焦點 //是否獲取焦點 @Override public boolean isFocused() { return true; } }

當然也可以在程式碼中設定獲取焦點,效果一樣。

 tvHome_marquee.setFocusable(true);