欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android開發(fā)之ViewSwitcher用法實例

 更新時間:2016年02月15日 10:15:18   作者:bigconvience  
這篇文章主要介紹了Android開發(fā)之ViewSwitcher用法,結(jié)合實例形式分析了ViewSwitcher的功能、使用方法與相關(guān)注意事項,需要的朋友可以參考下

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

android.widget.ViewSwitcher是ViewAnimator的子類,用于在兩個View之間切換,但每次只能顯示一個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ù)量超過兩個,會拋出異常:java.lang.IllegalStateException,打印 "Can't add more than 2 views to a ViewSwitcher" 。你可以使用ViewSwitcher的factory創(chuàng)建View或添加自己創(chuàng)建的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();
      }
    });
    ;
  }
}

實現(xiàn)效果圖:

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

第一步:獲得ViewSwithcer的實例

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

第二部:實現(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)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android控件用法總結(jié)》、《Android短信與電話操作技巧匯總》及《Android多媒體操作技巧匯總(音頻,視頻,錄音等)

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

相關(guān)文章

  • Android 界面開發(fā)顏色整理

    Android 界面開發(fā)顏色整理

    本文主要介紹Android 界面開發(fā)的顏色,這里整理了很多顏色以供大家參考,希望Android 開發(fā)的工作者可以參考使用
    2016-07-07
  • 設(shè)置界面開發(fā)Preference Library數(shù)據(jù)重建機(jī)制詳解

    設(shè)置界面開發(fā)Preference Library數(shù)據(jù)重建機(jī)制詳解

    這篇文章主要為大家介紹了設(shè)置界面開發(fā)利器Preference Library數(shù)據(jù)重建機(jī)制詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • Android實現(xiàn)狀態(tài)欄白底黑字效果示例代碼

    Android實現(xiàn)狀態(tài)欄白底黑字效果示例代碼

    這篇文章主要介紹了Android實現(xiàn)狀態(tài)欄白底黑字效果的相關(guān)資料,實現(xiàn)后的效果非常適合日常開發(fā)中使用,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-10-10
  • Android中使用achartengine生成圖表的具體方法

    Android中使用achartengine生成圖表的具體方法

    這篇文章主要介紹了Android中使用achartengine生成圖表的具體方法,有需要的朋友可以參考一下
    2014-01-01
  • Android 中倒計時驗證兩種常用方式實例詳解

    Android 中倒計時驗證兩種常用方式實例詳解

    這篇文章主要介紹了Android 中倒計時驗證兩種常用方式實例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Android?線程死鎖場景與優(yōu)化解決

    Android?線程死鎖場景與優(yōu)化解決

    線程死鎖是老生常談的問題,線程池死鎖本質(zhì)上屬于線程死鎖的一部分,線程池造成的死鎖問題往往和業(yè)務(wù)場景相關(guān),本文主要介紹了Android?線程死鎖場景與優(yōu)化,感興趣的可以了解一下
    2023-12-12
  • Java ArrayList源碼深入分析

    Java ArrayList源碼深入分析

    ArrayList 類是一個可以動態(tài)修改的數(shù)組,與普通數(shù)組的區(qū)別就是它是沒有固定大小的限制,我們可以添加或刪除元素。ArrayList 繼承了 AbstractList,并實現(xiàn)了List接口
    2022-08-08
  • Android 內(nèi)核代碼 wake_up源碼解析

    Android 內(nèi)核代碼 wake_up源碼解析

    這篇文章主要為大家介紹了Android 內(nèi)核代碼 wake_up源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Android開發(fā)的IDE、ADT、SDK、JDK、NDK等名詞解釋

    Android開發(fā)的IDE、ADT、SDK、JDK、NDK等名詞解釋

    這篇文章主要介紹了Android開發(fā)的IDE、ADT、SDK、JDK、NDK等名詞解釋,對這些概念搞不清楚是一件痛苦的事,本文就簡潔講解了這些名詞的含義,一起掃盲吧,需要的朋友可以參考下
    2015-06-06
  • Android本地視頻壓縮方案的示例代碼

    Android本地視頻壓縮方案的示例代碼

    本篇文章主要介紹了Android本地視頻壓縮方案的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01

最新評論