android使用TextView實現(xiàn)跑馬燈效果
更新時間:2020年05月19日 17:25:51 作者:小書包大夢想
這篇文章主要為大家詳細介紹了android使用TextView實現(xiàn)跑馬燈效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了android使用TextView實現(xiàn)跑馬燈效果的具體代碼,供大家參考,具體內容如下
先上效果圖:此為靜態(tài)圖,實際動態(tài)中文字勻速向左滑動。

實現(xiàn)步驟:
第一步:創(chuàng)建好布局頁面
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorAccent" android:textColor="#fff" android:textSize="15sp" android:padding="10dp" android:layout_margin="10dp"/> </android.support.constraint.ConstraintLayout>
第二步:在activity中編寫java代碼
package com.example.smallbag.autoscrolltext;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = (TextView) findViewById(R.id.textview);
String html = "1月25日上午,中共中央政治局在人民日報社就全媒體時代和媒體融合發(fā)展舉行第十二次集體學習。通過人民日報向全國的新聞工作者致以新春慰問和祝福。";
// CharSequence charSequence = Html.fromHtml(html); // 使文本具有html的功能,如超鏈接
textView.setText(html);
textView.setMovementMethod(LinkMovementMethod.getInstance()); // 添加手動滑動功能
textView.setEllipsize(TextUtils.TruncateAt.valueOf("MARQUEE")); // 添加跑馬燈功能
textView.setMarqueeRepeatLimit(Integer.MAX_VALUE); // 跑馬燈滾動次數(shù),此處已設置最大值
textView.setSingleLine(true); // 設置為單行顯示
textView.setFocusable(true); // 獲得焦點
textView.setFocusableInTouchMode(true); // 通過觸碰獲取焦點的能力
}
}
設置textview的屬性也可以直接在布局文件中設定,博主在布局文件中設置時出現(xiàn)了不能滾動的問題,原因未知,注意即可
第三步:運行程序,得到效果
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Android 中TextView中跑馬燈效果的實現(xiàn)方法
- Android TextView實現(xiàn)跑馬燈效果的方法
- Android自定義textview實現(xiàn)豎直滾動跑馬燈效果
- Android中使用TextView實現(xiàn)文字跑馬燈效果
- Android基于TextView不獲取焦點實現(xiàn)跑馬燈效果
- Android基于TextView屬性android:ellipsize實現(xiàn)跑馬燈效果的方法
- Android 實現(xiàn)不依賴焦點和選中的TextView跑馬燈
- Android自定義TextView跑馬燈效果
- Android使用TextView跑馬燈效果
- Android?TextView跑馬燈實現(xiàn)原理及方法實例
相關文章
Android使用Profiler查看應用內存分析的操作步驟
內存分析是Profiler中的一個組件,可以幫助我們識別可能會導致應用卡頓、凍結甚至崩潰的內存泄露和內存抖動,本文小編將給大家介紹一下Android使用Profiler查看應用內存分析的操作步驟,需要的朋友可以參考下2023-10-10
基于Flutter實現(xiàn)動態(tài)高斯模糊的流程步驟
一個App加上高斯模糊會形成一種高級的感覺,本文將介紹如何制作一個根據背景內容來動態(tài)高斯模糊,文中有詳細的代碼實現(xiàn)步驟,代碼示例講解的非常詳細,具有一定的參考價值,需要的朋友可以參考下2023-11-11

