App下載

在android中怎么實(shí)現(xiàn)跑馬燈效果效果?使用TextView實(shí)現(xiàn)跑馬燈效果方法分享!

君心似我心 2021-08-21 16:51:31 瀏覽數(shù) (2922)
反饋

對(duì)于在android開發(fā)中我們的問題解決之后就會(huì)又有一個(gè)接踵而至,那么今天我們就來說“在android中怎么實(shí)現(xiàn)跑馬燈效果效果?”這個(gè)較為基礎(chǔ)的問題的解決方法吧!

具體內(nèi)容如下

先上效果圖:此為靜態(tài)圖,實(shí)際動(dòng)態(tài)中文字勻速向左滑動(dòng)。

實(shí)現(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日上午,中共中央政治局在人民日?qǐng)?bào)社就全媒體時(shí)代和媒體融合發(fā)展舉行第十二次集體學(xué)習(xí)。通過人民日?qǐng)?bào)向全國的新聞工作者致以新春慰問和祝福。";
  // 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); // 通過觸碰獲取焦點(diǎn)的能力
 }
}

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

第三步:運(yùn)行程序,得到效果

那么在上面的描述中大家可以邊實(shí)操這樣可以加快自己對(duì)于:“在android中怎么實(shí)現(xiàn)跑馬燈效果效果?”這個(gè)問題的解決方法,更多關(guān)于android的內(nèi)容我們都可以在W3Cschool中進(jìn)行學(xué)習(xí)和了解。


0 人點(diǎn)贊