Android文本視圖TextView實現(xiàn)跑馬燈效果
本文實例為大家分享了Android文本視圖TextView實現(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; // 聲明一個文本視圖對象 private boolean isPaused = false; // 跑馬燈文字是否暫停滾動 @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è)置點擊監(jiān)聽器 tv_marquee.setOnClickListener(this); } @Override public void onClick(View v) { if (v.getId() == R.id.tv_marquee) { // 點擊了文本視圖tv_marquee isPaused = !isPaused; if (isPaused) { tv_marquee.setFocusable(false); // 不允許獲得焦點 tv_marquee.setFocusableInTouchMode(false); // 不允許在觸摸時獲得焦點 } else { tv_marquee.setFocusable(true); // 允許獲得焦點 tv_marquee.setFocusableInTouchMode(true); // 允許在觸摸時獲得焦點 tv_marquee.requestFocus(); // 強(qiáng)制獲得焦點,讓跑馬燈滾起來 } } } }
layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 這個是普通的文本視圖 --> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:gravity="center" android:text="跑馬燈效果,點擊暫停,再點擊恢復(fù)" /> <!-- 這個是跑馬燈滾動的文本視圖,ellipsize屬性設(shè)置為true表示文字從右向左滾動 --> <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)臺風(fēng)“莫蘭蒂”即將登陸,請居民關(guān)緊門窗、備足糧草,做好防汛救災(zāi)準(zhǔn)備!" /> </LinearLayout>
result
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android?studio實驗:?UI設(shè)計?ListView及事件響應(yīng)
這篇文章主要介紹了android?studio實驗:?UI設(shè)計?ListView及事件響應(yīng),主要是ListView及其事件響應(yīng)方法?彈出菜單PopupMenu及其事件響應(yīng)方法,下面來看看具文章體的介紹吧2021-12-12Android使用Notification實現(xiàn)通知功能
這篇文章主要為大家詳細(xì)介紹了Android使用Notification實現(xiàn)通知功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11安卓(Android)聊天機(jī)器人實現(xiàn)代碼分享
這是一個安卓智能聊天機(jī)器人的源碼,采用了仿微信的風(fēng)格設(shè)計,調(diào)用的是圖靈機(jī)器人的API,能夠?qū)崿F(xiàn)智能聊天、講故事、講笑話、查天氣、查公交等豐富的功能2015-11-11Android TextWatcher三個回調(diào)以及監(jiān)聽EditText的輸入案例詳解
這篇文章主要介紹了Android TextWatcher三個回調(diào)以及監(jiān)聽EditText的輸入案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08Android與H5交互產(chǎn)生Script Error踩坑解決
這篇文章主要為大家介紹了Android與H5交互產(chǎn)生Script Error問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Android中NavigationView的使用與相關(guān)問題解決
大家都知道NavigationView的引入讓 Android側(cè)邊欄實現(xiàn)起來相當(dāng)方便,最近公司項目中也使用這個新的控件完成了側(cè)邊欄的改版。在使用過程中遇到一些問題所以記錄一下。本文分為兩個部分,一是基本使用,二是相關(guān)問題的解決,感興趣的朋友們下面來一起看看吧。2016-10-10Android NotificationManager簡單使用詳解
這篇文章主要為大家詳細(xì)介紹了Android NotificationManager的簡單使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11