Android TextSwitcher文本切換器和ViewFlipper使用詳解
本文為大家分享了Android TextSwitcher文本切換器的使用,供大家參考,具體內(nèi)容如下

1.TextSwitcher
使用:
應(yīng)用分為三步:
1.得到 TextSwitcher 實(shí)例對(duì)象
TextSwitcher switcher = (TextSwitcher) findViewById(R.id.textSwitcher);
2.為switcher指定ViewSwitcher.ViewFactory工廠,該工廠會(huì)產(chǎn)生出轉(zhuǎn)換時(shí)需要的View
switcher.setFactory(this);
3.為switcher設(shè)定顯示的內(nèi)容,該方法執(zhí)行,就會(huì)切換到下個(gè)View
switcher.setText(String.valueOf(new Random().nextInt()));

2.ViewFlipper

實(shí)現(xiàn):
①創(chuàng)建主布局文件,包含ViewFlipper控件(從源碼來(lái)看,ViewFlipper控件是集成FrameLayout,也是相當(dāng)于一個(gè)幀布局,只是包含了一些特殊的屬性)
②創(chuàng)建ViewFlipper的子View,就是兩個(gè)LinearLayout(里面包含兩個(gè)TextView)
③創(chuàng)建ViewFlipper中子view的進(jìn)入和退出動(dòng)畫(huà)anim_in和anim_out兩個(gè)動(dòng)畫(huà)文件
④在Activity中將兩個(gè)子View添加到ViewFlipper中去,調(diào)動(dòng)的是ViewFlipper的addView方法
具體代碼:
①創(chuàng)建主布局文件
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
android:fitsSystemWindows="true"
tools:context="com.qianmo.rollads.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.qianmo.rollads.MainActivity"
tools:showIn="@layout/activity_main">
<ViewFlipper
android:id="@+id/viewFlipper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoStart="true"
android:background="#fff"
android:flipInterval="3000"
android:inAnimation="@anim/anim_in"
android:outAnimation="@anim/anim_out"
android:paddingLeft="30dp"
>
</ViewFlipper>
</RelativeLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email"/>
</android.support.design.widget.CoordinatorLayout>
②創(chuàng)建ViewFlipper的子View,這里有兩個(gè),我就只給出一個(gè)來(lái)了,另一個(gè)是一樣的
one_ads.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="8dp"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="熱議"
android:textColor="#F14C00"
android:textSize="12sp"
android:background="@drawable/bg"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:padding="3dp"
android:singleLine="true"
android:text="小米8問(wèn)世,雷胖子現(xiàn)在笑的開(kāi)心了啦!"
android:textColor="#333"
android:textSize="16sp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="熱議"
android:textColor="#F14C00"
android:textSize="12sp"
android:background="@drawable/bg"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:padding="3dp"
android:singleLine="true"
android:text="取了個(gè)漂亮的媳婦,整的是好開(kāi)心!"
android:textColor="#333"
android:textSize="16sp"
/>
</LinearLayout>
</LinearLayout>
③創(chuàng)建ViewFlipper中子view的進(jìn)入和退出動(dòng)畫(huà)anim_in和anim_out兩個(gè)動(dòng)畫(huà)文件,這里也只給出anim_in的代碼,anim_out代碼是類(lèi)似的
anim_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1500"
android:fromYDelta="100%p"
android:toYDelta="0"
/>
</set>
④在Activity中將兩個(gè)子View添加到ViewFlipper中去,調(diào)動(dòng)的是ViewFlipper的addView方法
MainActivity.java
package com.qianmo.rollads;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ViewFlipper;
public class MainActivity extends AppCompatActivity {
private ViewFlipper viewFlipper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
viewFlipper.addView(View.inflate(this, R.layout.one_ads, null));
viewFlipper.addView(View.inflate(this, R.layout.two_ads, null));
}
}
ok,來(lái)看一下我們的效果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 上下滾動(dòng)TextSwitcher實(shí)例詳解
- Android TextSwitcher實(shí)現(xiàn)文字上下翻牌效果(銅板街)
- Android App中用Handler實(shí)現(xiàn)ViewPager頁(yè)面的自動(dòng)切換
- Android應(yīng)用中圖片瀏覽時(shí)實(shí)現(xiàn)自動(dòng)切換功能的方法詳解
- Android開(kāi)發(fā)之使用ViewPager實(shí)現(xiàn)圖片左右滑動(dòng)切換效果
- Android App仿微信界面切換時(shí)Tab圖標(biāo)變色效果的制作方法
- Android自定義ImageView實(shí)現(xiàn)點(diǎn)擊兩張圖片切換效果
- Android實(shí)現(xiàn)圖片輪播切換實(shí)例代碼
- Android編程實(shí)現(xiàn)圖片背景漸變切換與圖層疊加效果
- Android實(shí)現(xiàn)加載狀態(tài)視圖切換效果
- Android開(kāi)發(fā)實(shí)現(xiàn)自動(dòng)切換文字TextSwitcher功能示例
相關(guān)文章
Android Studio 3.6中新的視圖綁定工具ViewBinding 用法詳解
這篇文章主要介紹了Android Studio 3.6中新的視圖綁定工具ViewBinding 用法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
android使用AsyncTask實(shí)現(xiàn)多線程下載實(shí)例
這篇文章主要介紹了android使用AsyncTask實(shí)現(xiàn)多線程下載實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
Android獲取應(yīng)用程序名稱(ApplicationName)示例
本文以實(shí)例方式為大家介紹下獲取應(yīng)用程序名稱(ApplicationName)的具體實(shí)現(xiàn),感興趣的各位可以參考下哈2013-06-06
Android7.0實(shí)現(xiàn)拍照和相冊(cè)選取圖片功能
這篇文章主要為大家詳細(xì)介紹了Android7.0實(shí)現(xiàn)拍照和相冊(cè)選取圖片功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Android自定義view實(shí)現(xiàn)拖動(dòng)小球移動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android自定義view實(shí)現(xiàn)拖動(dòng)小球移動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
Android搭建本地Tomcat服務(wù)器及相關(guān)配置
這篇文章主要介紹了Android搭建本地Tomcat服務(wù)器及相關(guān)配置,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Kotlin Option與Either及Result實(shí)現(xiàn)異常處理詳解
Kotlin異常處理,異常是在程序運(yùn)行時(shí)可能發(fā)生的不必要的問(wèn)題,并突然終止您的程序。異常處理是一個(gè)過(guò)程,使用它可以防止程序出現(xiàn)可能破壞我們代碼的異常2022-12-12

