Android文本視圖TextView實(shí)現(xiàn)跑馬燈效果
本文實(shí)例為大家分享了Android文本視圖TextView實(shí)現(xiàn)跑馬燈效果的具體代碼,供大家參考,具體內(nèi)容如下
MainActivity
package com.example.junior;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MarqueeActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tv_marquee; // 聲明一個(gè)文本視圖對(duì)象
private boolean isPaused = false; // 跑馬燈文字是否暫停滾動(dòng)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_marquee);
// 從布局文件中獲取名叫tv_marquee的文本視圖
tv_marquee = findViewById(R.id.tv_marquee);
// 給tv_marquee設(shè)置點(diǎn)擊監(jiān)聽(tīng)器
tv_marquee.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.tv_marquee) { // 點(diǎn)擊了文本視圖tv_marquee
isPaused = !isPaused;
if (isPaused) {
tv_marquee.setFocusable(false); // 不允許獲得焦點(diǎn)
tv_marquee.setFocusableInTouchMode(false); // 不允許在觸摸時(shí)獲得焦點(diǎn)
} else {
tv_marquee.setFocusable(true); // 允許獲得焦點(diǎn)
tv_marquee.setFocusableInTouchMode(true); // 允許在觸摸時(shí)獲得焦點(diǎn)
tv_marquee.requestFocus(); // 強(qiáng)制獲得焦點(diǎn),讓跑馬燈滾起來(lái)
}
}
}
}
layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 這個(gè)是普通的文本視圖 -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="跑馬燈效果,點(diǎn)擊暫停,再點(diǎn)擊恢復(fù)" />
<!-- 這個(gè)是跑馬燈滾動(dòng)的文本視圖,ellipsize屬性設(shè)置為true表示文字從右向左滾動(dòng) -->
<TextView
android:id="@+id/tv_marquee"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:textColor="#000000"
android:textSize="17sp"
android:text="快訊:紅色預(yù)警,超強(qiáng)臺(tái)風(fēng)“莫蘭蒂”即將登陸,請(qǐng)居民關(guān)緊門(mén)窗、備足糧草,做好防汛救災(zāi)準(zhǔn)備!" />
</LinearLayout>
result

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android短信驗(yàn)證碼自動(dòng)填寫(xiě)實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android短信驗(yàn)證碼自動(dòng)填寫(xiě)實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android開(kāi)發(fā)之SQLite的使用方法
本篇文章介紹了,Android開(kāi)發(fā)之SQLite的使用方法。需要的朋友參考下2013-04-04
android?studio實(shí)驗(yàn):?UI設(shè)計(jì)?ListView及事件響應(yīng)
這篇文章主要介紹了android?studio實(shí)驗(yàn):?UI設(shè)計(jì)?ListView及事件響應(yīng),主要是ListView及其事件響應(yīng)方法?彈出菜單PopupMenu及其事件響應(yīng)方法,下面來(lái)看看具文章體的介紹吧2021-12-12
Android使用Notification實(shí)現(xiàn)通知功能
這篇文章主要為大家詳細(xì)介紹了Android使用Notification實(shí)現(xiàn)通知功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
安卓(Android)聊天機(jī)器人實(shí)現(xiàn)代碼分享
這是一個(gè)安卓智能聊天機(jī)器人的源碼,采用了仿微信的風(fēng)格設(shè)計(jì),調(diào)用的是圖靈機(jī)器人的API,能夠?qū)崿F(xiàn)智能聊天、講故事、講笑話、查天氣、查公交等豐富的功能2015-11-11
Android TextWatcher三個(gè)回調(diào)以及監(jiān)聽(tīng)EditText的輸入案例詳解
這篇文章主要介紹了Android TextWatcher三個(gè)回調(diào)以及監(jiān)聽(tīng)EditText的輸入案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Android與H5交互產(chǎn)生Script Error踩坑解決
這篇文章主要為大家介紹了Android與H5交互產(chǎn)生Script Error問(wèn)題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Android中NavigationView的使用與相關(guān)問(wèn)題解決
大家都知道NavigationView的引入讓 Android側(cè)邊欄實(shí)現(xiàn)起來(lái)相當(dāng)方便,最近公司項(xiàng)目中也使用這個(gè)新的控件完成了側(cè)邊欄的改版。在使用過(guò)程中遇到一些問(wèn)題所以記錄一下。本文分為兩個(gè)部分,一是基本使用,二是相關(guān)問(wèn)題的解決,感興趣的朋友們下面來(lái)一起看看吧。2016-10-10
Android NotificationManager簡(jiǎn)單使用詳解
這篇文章主要為大家詳細(xì)介紹了Android NotificationManager的簡(jiǎn)單使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11

