溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

在android中如何實現(xiàn)跑馬燈效果效果

發(fā)布時間:2022-02-25 14:40:44 來源:億速云 閱讀:181 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)在android中如何實現(xiàn)跑馬燈效果效果的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

在android中如何實現(xiàn)跑馬燈效果效果

實現(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ù),此處已設(shè)置最大值
  textView.setSingleLine(true); // 設(shè)置為單行顯示
  textView.setFocusable(true); // 獲得焦點
  textView.setFocusableInTouchMode(true); // 通過觸碰獲取焦點的能力
 }
}

設(shè)置textview的屬性也可以直接在布局文件中設(shè)定,博主在布局文件中設(shè)置時出現(xiàn)了不能滾動的問題,原因未知,注意即可

感謝各位的閱讀!關(guān)于“在android中如何實現(xiàn)跑馬燈效果效果”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI