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)聽器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è)是普通的文本視圖 --> <TextViewandroid: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) --> <TextViewandroid: 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)緊門窗、備足糧草,做好防汛救災(zāi)準(zhǔn)備!' /></LinearLayout>
result
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python:刪除離群值操作(每一行為一類數(shù)據(jù))2. 使用ProcessBuilder調(diào)用外部命令,并返回大量結(jié)果3. 詳解Vue中Axios封裝API接口的思路及方法4. JSP實(shí)現(xiàn)客戶信息管理系統(tǒng)5. python中if嵌套命令實(shí)例講解6. python 批量下載bilibili視頻的gui程序7. python 通過exifread讀取照片信息8. 使用css實(shí)現(xiàn)全兼容tooltip提示框9. CSS自定義滾動(dòng)條樣式案例詳解10. python中HTMLParser模塊知識(shí)點(diǎn)總結(jié)
