歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android ViewPager控件的使用【附源碼】

Android ViewPager控件的使用【附源碼】

日期:2017/3/1 10:19:20   编辑:Linux編程

Android 4.0有個控件,就是viewpager,用來實現左右滑動效果的。我們具體來看看是如何使用的。

首先看一下效果圖

3個tab,每個tab有個layout,滑動的時候指示滑動條跟著走(當然,我覺得這個滑動條可以用三張圖片來實現,更簡單點)。

具體的我們看代碼吧,主頁xml就是實現上面三個tabbar,滑動條和一個viewpager控件,如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <LinearLayout
  7. android:id="@+id/linearLayout1"
  8. android:layout_width="fill_parent"
  9. android:layout_height="100.0dip"
  10. android:background="#FFFFFF" >
  11. <TextView
  12. android:id="@+id/text1"
  13. android:layout_width="fill_parent"
  14. android:layout_height="fill_parent"
  15. android:layout_weight="1.0"
  16. android:gravity="center"
  17. android:text="頁卡1"
  18. android:textColor="#000000"
  19. android:textSize="22.0dip" />
  20. <TextView
  21. android:id="@+id/text2"
  22. android:layout_width="fill_parent"
  23. android:layout_height="fill_parent"
  24. android:layout_weight="1.0"
  25. android:gravity="center"
  26. android:text="頁卡2"
  27. android:textColor="#000000"
  28. android:textSize="22.0dip" />
  29. <TextView
  30. android:id="@+id/text3"
  31. android:layout_width="fill_parent"
  32. android:layout_height="fill_parent"
  33. android:layout_weight="1.0"
  34. android:gravity="center"
  35. android:text="頁卡3"
  36. android:textColor="#000000"
  37. android:textSize="22.0dip" />
  38. </LinearLayout>
  39. <ImageView
  40. android:id="@+id/cursor"
  41. android:layout_width="fill_parent"
  42. android:layout_height="wrap_content"
  43. android:scaleType="matrix"
  44. android:src="@drawable/slide" />
  45. <android.support.v4.view.ViewPager
  46. android:id="@+id/vPager"
  47. android:layout_width="wrap_content"
  48. android:layout_height="wrap_content"
  49. android:layout_gravity="center"
  50. android:layout_weight="1.0"
  51. android:background="#000000"
  52. android:flipInterval="30"
  53. android:persistentDrawingCache="animation" />
  54. </LinearLayout>
Copyright © Linux教程網 All Rights Reserved