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è)是普通的文本視圖 --> <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. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)2. Jsp+Servlet實(shí)現(xiàn)文件上傳下載 文件列表展示(二)3. XML在語(yǔ)音合成中的應(yīng)用4. Jsp servlet驗(yàn)證碼工具類分享5. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)車輛管理系統(tǒng)6. jscript與vbscript 操作XML元素屬性的代碼7. 基于PHP做個(gè)圖片防盜鏈8. ASP.NET MVC使用Boostrap實(shí)現(xiàn)產(chǎn)品展示、查詢、排序、分頁(yè)9. ASP將數(shù)字轉(zhuǎn)中文數(shù)字(大寫金額)的函數(shù)10. asp.net core 認(rèn)證和授權(quán)實(shí)例詳解
