android使用TextView實(shí)現(xiàn)跑馬燈效果
本文實(shí)例為大家分享了android使用TextView實(shí)現(xiàn)跑馬燈效果的具體代碼,供大家參考,具體內(nèi)容如下
先上效果圖:此為靜態(tài)圖,實(shí)際動(dòng)態(tài)中文字勻速向左滑動(dòng)。

實(shí)現(xiàn)步驟:
第一步:創(chuàng)建好布局頁(yè)面
<?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中編寫(xiě)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日上午,中共中央政治局在人民日?qǐng)?bào)社就全媒體時(shí)代和媒體融合發(fā)展舉行第十二次集體學(xué)習(xí)。通過(guò)人民日?qǐng)?bào)向全國(guó)的新聞工作者致以新春慰問(wèn)和祝福。";
// CharSequence charSequence = Html.fromHtml(html); // 使文本具有html的功能,如超鏈接
textView.setText(html);
textView.setMovementMethod(LinkMovementMethod.getInstance()); // 添加手動(dòng)滑動(dòng)功能
textView.setEllipsize(TextUtils.TruncateAt.valueOf("MARQUEE")); // 添加跑馬燈功能
textView.setMarqueeRepeatLimit(Integer.MAX_VALUE); // 跑馬燈滾動(dòng)次數(shù),此處已設(shè)置最大值
textView.setSingleLine(true); // 設(shè)置為單行顯示
textView.setFocusable(true); // 獲得焦點(diǎn)
textView.setFocusableInTouchMode(true); // 通過(guò)觸碰獲取焦點(diǎn)的能力
}
}
設(shè)置textview的屬性也可以直接在布局文件中設(shè)定,博主在布局文件中設(shè)置時(shí)出現(xiàn)了不能滾動(dòng)的問(wèn)題,原因未知,注意即可
第三步:運(yùn)行程序,得到效果
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 中TextView中跑馬燈效果的實(shí)現(xiàn)方法
- Android TextView實(shí)現(xiàn)跑馬燈效果的方法
- Android自定義textview實(shí)現(xiàn)豎直滾動(dòng)跑馬燈效果
- Android中使用TextView實(shí)現(xiàn)文字跑馬燈效果
- Android基于TextView不獲取焦點(diǎn)實(shí)現(xiàn)跑馬燈效果
- Android基于TextView屬性android:ellipsize實(shí)現(xiàn)跑馬燈效果的方法
- Android 實(shí)現(xiàn)不依賴焦點(diǎn)和選中的TextView跑馬燈
- Android自定義TextView跑馬燈效果
- Android使用TextView跑馬燈效果
- Android?TextView跑馬燈實(shí)現(xiàn)原理及方法實(shí)例
相關(guān)文章
Android基于TextView屬性android:ellipsize實(shí)現(xiàn)跑馬燈效果的方法
這篇文章主要介紹了Android基于TextView屬性android:ellipsize實(shí)現(xiàn)跑馬燈效果的方法,涉及Android跑馬燈效果所涉及的TextView相關(guān)屬性與使用方法,需要的朋友可以參考下2016-08-08
詳解Android App中的AsyncTask異步任務(wù)執(zhí)行方式
這篇文章主要介紹了Android App中的AsyncTask異步任務(wù)執(zhí)行方式,文中舉了一個(gè)打開(kāi)網(wǎng)絡(luò)圖片的例子幫助大家直觀理解,需要的朋友可以參考下2016-04-04
Android中ScrollView嵌套GridView的解決辦法
前些日子在開(kāi)發(fā)中用到了需要ScrollView嵌套GridView的情況,由于這兩款控件都自帶滾動(dòng)條,當(dāng)他們碰到一起的時(shí)候便會(huì)出問(wèn)題,即GridView會(huì)顯示不全。下面小編給大家分享下解決方案,需要的朋友可以參考下2017-04-04
android獲取及監(jiān)聽(tīng)手機(jī)網(wǎng)絡(luò)狀態(tài)
大家好,本篇文章主要講的是android獲取及監(jiān)聽(tīng)手機(jī)網(wǎng)絡(luò)狀態(tài),感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2022-01-01
Android 配置gradle實(shí)現(xiàn)VersionCode自增實(shí)例
今天小編就為大家分享一篇Android 配置gradle實(shí)現(xiàn)VersionCode自增實(shí)例,具有很好的 參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
android listview初步學(xué)習(xí)實(shí)例代碼
這篇文章主要介紹了android listview初步學(xué)習(xí)實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
Android編程中PopupWindow的用法分析【位置、動(dòng)畫(huà)、焦點(diǎn)】
這篇文章主要介紹了Android編程中PopupWindow的用法,結(jié)合實(shí)例形式分析了PopupWindow控件位置、動(dòng)畫(huà)、焦點(diǎn)等操作相關(guān)技巧,需要的朋友可以參考下2017-02-02
Android編程實(shí)現(xiàn)創(chuàng)建,刪除,判斷快捷方式的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)創(chuàng)建,刪除,判斷快捷方式的方法,結(jié)合實(shí)例形式分析了Android編程針對(duì)快捷方式的常用操作技巧,需要的朋友可以參考下2017-02-02

