国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android開發之ViewSwitcher用法實例

2020-04-11 10:49:33
字體:
來源:轉載
供稿:網友

本文實例講述了Android開發之ViewSwitcher用法。分享給大家供大家參考,具體如下:

android.widget.ViewSwitcher是ViewAnimator的子類,用于在兩個View之間切換,但每次只能顯示一個View。

ViewSwitcher的addView函數的代碼如下:

/** * {@inheritDoc} * * @throws IllegalStateException if this switcher already contains two children */@Overridepublic 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的數量超過兩個,會拋出異常:java.lang.IllegalStateException,打印 "Can't add more than 2 views to a ViewSwitcher" 。你可以使用ViewSwitcher的factory創建View或添加自己創建的View。

下面用一個例子介紹一下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();      }    });    ;  }}

實現效果圖:

使用ViewSwitcher的setFactory設置切換的View,分為兩步。

第一步:獲得ViewSwithcer的實例

switcher = (ViewSwitcher) findViewById(R.id.viewSwitcher);

第二部:實現接口ViewFactory

switcher.setFactory(new ViewFactory(){  @Override  public View makeView()  {    return inflater.inflate(R.layout.slidelistview, null);  }});

更多關于Android相關內容感興趣的讀者可查看本站專題:《Android開發入門與進階教程》、《Android控件用法總結》、《Android短信與電話操作技巧匯總》及《Android多媒體操作技巧匯總(音頻,視頻,錄音等)

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 龙胜| 鹿泉市| 大悟县| 茌平县| 长子县| 北票市| 上饶县| 岑巩县| 马鞍山市| 洛南县| 综艺| 怀仁县| 大理市| 黎平县| 长武县| 南江县| 聂荣县| 香港| 铁力市| 德兴市| 海门市| 浦县| 广昌县| 蒙山县| 莆田市| 山东| 崇礼县| 微山县| 泰州市| 昭平县| 红安县| 林甸县| 秦安县| 阿图什市| 贡嘎县| 黔西县| 云南省| 邵东县| 达日县| 教育| 焉耆|