Android開(kāi)發(fā)之ViewSwitcher用法實(shí)例
本文實(shí)例講述了Android開(kāi)發(fā)之ViewSwitcher用法。分享給大家供大家參考,具體如下:
android.widget.ViewSwitcher是ViewAnimator的子類(lèi),用于在兩個(gè)View之間切換,但每次只能顯示一個(gè)View。
ViewSwitcher的addView函數(shù)的代碼如下:
/**
* {@inheritDoc}
*
* @throws IllegalStateException if this switcher already contains two children
*/
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
if (getChildCount() >= 2) {
throw new IllegalStateException("Can't add more than 2 views to a ViewSwitcher");
}
super.addView(child, index, params);
}
可以看出,若View的數(shù)量超過(guò)兩個(gè),會(huì)拋出異常:java.lang.IllegalStateException,打印 "Can't add more than 2 views to a ViewSwitcher" 。你可以使用ViewSwitcher的factory創(chuàng)建View或添加自己創(chuàng)建的View。
下面用一個(gè)例子介紹一下ViewSwitcher的用法。
布局文件:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/prev"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="previous" />
<Button
android:id="@+id/next"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="next" />
</LinearLayout>
<ViewSwitcher
android:id="@+id/viewswitcher"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="- Button 2 -" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LinearLayout 2" />
</LinearLayout>
</ViewSwitcher>
</LinearLayout>
Activity的代碼:
package com.example.AndroidTest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ViewSwitcher;
public class MyActivity extends Activity {
Button buttonPrev, buttonNext;
ViewSwitcher viewSwitcher;
Animation slide_in_left, slide_out_right;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonPrev = (Button) findViewById(R.id.prev);
buttonNext = (Button) findViewById(R.id.next);
viewSwitcher = (ViewSwitcher) findViewById(R.id.viewswitcher);
slide_in_left = AnimationUtils.loadAnimation(this,
android.R.anim.slide_in_left);
slide_out_right = AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right);
viewSwitcher.setInAnimation(slide_in_left);
viewSwitcher.setOutAnimation(slide_out_right);
buttonPrev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
viewSwitcher.showPrevious();
}
});
buttonNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
viewSwitcher.showNext();
}
});
;
}
}
實(shí)現(xiàn)效果圖:

使用ViewSwitcher的setFactory設(shè)置切換的View,分為兩步。
第一步:獲得ViewSwithcer的實(shí)例
switcher = (ViewSwitcher) findViewById(R.id.viewSwitcher);
第二部:實(shí)現(xiàn)接口ViewFactory
switcher.setFactory(new ViewFactory()
{
@Override
public View makeView()
{
return inflater.inflate(R.layout.slidelistview, null);
}
});
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android控件用法總結(jié)》、《Android短信與電話操作技巧匯總》及《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- android開(kāi)發(fā)教程之switch控件使用示例
- android基本控件ToggleButton&Switch使用指南
- Android入門(mén)之Gallery+ImageSwitcher用法實(shí)例解析
- Android UI設(shè)計(jì)系列之自定義SwitchButton開(kāi)關(guān)實(shí)現(xiàn)類(lèi)似IOS中UISwitch的動(dòng)畫(huà)效果(2)
- 很贊的引導(dǎo)界面效果Android控件ImageSwitcher實(shí)現(xiàn)
- Android控件ImageSwitcher實(shí)現(xiàn)左右圖片切換功能
- Android基于ImageSwitcher實(shí)現(xiàn)圖片切換功能
- Android中使用GridView和ImageViewSwitcher實(shí)現(xiàn)電子相冊(cè)簡(jiǎn)單功能實(shí)例
- Android使用gallery和imageSwitch制作可左右循環(huán)滑動(dòng)的圖片瀏覽器
- Android UI控件Switch的使用方法
相關(guān)文章
Android 藍(lán)牙自動(dòng)匹配PIN碼跳過(guò)用戶(hù)交互示例
本篇文章主要介紹了Android 藍(lán)牙自動(dòng)匹配PIN碼跳過(guò)用戶(hù)交互示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
Android ListView與RecycleView的對(duì)比使用解析
這篇文章主要介紹了Android ListView與RecycleView的對(duì)比使用解析,需要的朋友可以參考下2017-12-12
Flutter?點(diǎn)擊兩次退出app的實(shí)現(xiàn)示例
本文主要介紹了Flutter?點(diǎn)擊兩次退出app的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
Android自定義控件eBook實(shí)現(xiàn)翻書(shū)效果實(shí)例詳解
這篇文章主要介紹了Android自定義控件eBook實(shí)現(xiàn)翻書(shū)效果的方法,結(jié)合實(shí)例形式分析了Android自定義控件實(shí)現(xiàn)翻書(shū)效果的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2016-10-10
Android實(shí)現(xiàn)判斷某個(gè)服務(wù)是否正在運(yùn)行的方法
這篇文章主要介紹了Android實(shí)現(xiàn)判斷某個(gè)服務(wù)是否正在運(yùn)行的方法,涉及Android針對(duì)系統(tǒng)服務(wù)運(yùn)行狀態(tài)的判斷技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android 自定義驗(yàn)證碼輸入框的實(shí)例代碼(支持粘貼連續(xù)性)
這篇文章主要介紹了Android 自定義驗(yàn)證碼輸入框的實(shí)例代碼(支持粘貼連續(xù)性),代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
android 網(wǎng)絡(luò)編程之網(wǎng)絡(luò)通信幾種方式實(shí)例分享
這篇文章主要介紹了android 網(wǎng)絡(luò)編程之網(wǎng)絡(luò)通信幾種方式,有需要的朋友可以參考一下2013-12-12

