日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

TextView跑步灯效果及在特殊情况下无效的解决方案

發布時間:2025/3/20 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 TextView跑步灯效果及在特殊情况下无效的解决方案 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

概述:

? 關于在TextView中使用跑馬燈效果的例子在網上一搜一大把。他們可能會讓你像下面這樣來在xml中定義TextView控件的屬性。而事實也確是如此。不過我不知道他們有沒有遇到和我一樣的問題(其實我感覺是有的),我們第一次運行程序的時候,跑馬燈沒有效果,當我們關閉activity或是fragment再次進入的時候,跑馬燈的效果又有了。


一般情況:


<TextViewandroid:id="@+id/textview1"android:layout_width="200dp"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_toRightOf="@id/main_has_connected_textView"android:text="TextView"android:singleLine="true"android:ellipsize="marquee"android:focusable="true"android:marqueeRepeatLimit="marquee_forever"android:focusableInTouchMode="true"android:scrollHorizontally="true"android:textSize="22sp" />

修改之后:

如上的代碼,一些基本的該設置的屬性都已經設置好了。不過還是會出現第一次運行無效果的情況。這種情況出現的原因應該是TextView在獲得焦點的時候,會有丟失。我們可以動態地為這個TextView添加一些事件。不過為了方便和安全性,我們可以將其放在它的自定義控件中。

這個時候我們就需要在java代碼中來動態實現了。如下:

public class FlowTextView extends TextView {public FlowTextView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}public FlowTextView(Context context, AttributeSet attrs) {super(context, attrs);}public FlowTextView(Context context) {super(context);}@Overridepublic boolean isFocused() {return true;}}


xml中的使用與之前的無差別,如下:

<com.demo.widgets.FlowTextViewandroid:id="@+id/main_connect_fs_name"android:layout_width="200dp"android:layout_height="wrap_content"android:layout_centerVertical="true"android:text="TextView"android:singleLine="true"android:textColor="#ffFFFFFF"android:ellipsize="marquee"android:focusable="true"android:marqueeRepeatLimit="marquee_forever"android:focusableInTouchMode="true"android:scrollHorizontally="true"android:textSize="22sp" />


《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的TextView跑步灯效果及在特殊情况下无效的解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。