1.头部布局文件top.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="40dp"
  5. android:background="@drawable/title_bar"
  6. android:gravity="center"
  7. android:orientation="vertical" >
  8.  
  9. <TextView
  10. android:id="@+id/title"
  11. android:layout_width="wrap_content"
  12. android:layout_height="30dp"
  13. android:layout_gravity="center"
  14. android:text="WeChat"
  15. android:textColor="#ffffffff"
  16. android:textSize="25dp" />
  17.  
  18. </LinearLayout>

2.底部布局文件bottom.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="50dp"
  5. android:background="@drawable/bottom_bar"
  6. android:orientation="horizontal" >
  7.  
  8. <LinearLayout
  9. android:id="@+id/ll_chat"
  10. android:layout_width="0dp"
  11. android:layout_height="fill_parent"
  12. android:layout_weight="1"
  13. android:gravity="center"
  14. android:orientation="vertical" >
  15.  
  16. <ImageButton
  17. android:id="@+id/ibtn_chat"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:background="#0000"
  21. android:clickable="false"
  22. android:src="@drawable/tab_weixin_pressed" />
  23.  
  24. <TextView
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:text="聊天"
  28. android:textColor="#ffffffff" />
  29. </LinearLayout>
  30.  
  31. <LinearLayout
  32. android:id="@+id/ll_pengyouquan"
  33. android:layout_width="0dp"
  34. android:layout_height="fill_parent"
  35. android:layout_weight="1"
  36. android:gravity="center"
  37. android:orientation="vertical" >
  38.  
  39. <ImageButton
  40. android:id="@+id/ibtn_pengyouquan"
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:background="#0000"
  44. android:clickable="false"
  45. android:src="@drawable/tab_find_frd_normal" />
  46.  
  47. <TextView
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content"
  50. android:text="朋友圈"
  51. android:textColor="#ffffffff" />
  52. </LinearLayout>
  53.  
  54. <LinearLayout
  55. android:id="@+id/ll_tongxunlu"
  56. android:layout_width="0dp"
  57. android:layout_height="fill_parent"
  58. android:layout_weight="1"
  59. android:gravity="center"
  60. android:orientation="vertical" >
  61.  
  62. <ImageButton
  63. android:id="@+id/ibtn_tongxunlu"
  64. android:layout_width="wrap_content"
  65. android:layout_height="wrap_content"
  66. android:background="#0000"
  67. android:clickable="false"
  68. android:src="@drawable/tab_address_normal" />
  69.  
  70. <TextView
  71. android:layout_width="wrap_content"
  72. android:layout_height="wrap_content"
  73. android:text="好友"
  74. android:textColor="#ffffffff" />
  75. </LinearLayout>
  76.  
  77. <LinearLayout
  78. android:id="@+id/ll_set"
  79. android:layout_width="0dp"
  80. android:layout_height="fill_parent"
  81. android:layout_weight="1"
  82. android:gravity="center"
  83. android:orientation="vertical" >
  84.  
  85. <ImageButton
  86. android:id="@+id/ibtn_set"
  87. android:layout_width="wrap_content"
  88. android:layout_height="wrap_content"
  89. android:background="#0000"
  90. android:clickable="false"
  91. android:src="@drawable/tab_settings_normal" />
  92.  
  93. <TextView
  94. android:layout_width="wrap_content"
  95. android:layout_height="wrap_content"
  96. android:text="设置"
  97. android:textColor="#ffffffff" />
  98. </LinearLayout>
  99.  
  100. </LinearLayout>

3.主布局文件:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6.  
  7. <include layout="@layout/top" />
  8.  
  9. <android.support.v4.view.ViewPager
  10. android:id="@+id/viewpager"
  11. android:layout_width="match_parent"
  12. android:layout_height="0dp"
  13. android:layout_weight="1" >
  14. </android.support.v4.view.ViewPager>
  15.  
  16. <include layout="@layout/bottom" />
  17.  
  18. </LinearLayout>

4.四个ViewPager的内容页布局

tab01.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:gravity="center"
  6. android:orientation="vertical" >
  7.  
  8. <TextView
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_gravity="center"
  12. android:text="聊天页"
  13. android:textSize="30sp" >
  14. </TextView>
  15.  
  16. </LinearLayout>

tab02.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:gravity="center"
  6. android:orientation="vertical" >
  7.  
  8. <TextView
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_gravity="center"
  12. android:text="朋友圈"
  13. android:textSize="30sp" >
  14. </TextView>
  15.  
  16. </LinearLayout>

tab03.xml:

  1. <?
  2.  
  3. xml version="1.0" encoding="utf-8"?>
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:gravity="center"
  8. android:orientation="vertical" >
  9.  
  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_gravity="center"
  14. android:text="联系人页面"
  15. android:textSize="30sp" >
  16. </TextView>
  17.  
  18. </LinearLayout>

tab04.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:gravity="center"
  6. android:orientation="vertical" >
  7.  
  8. <TextView
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_gravity="center"
  12. android:text="设置页面"
  13. android:textSize="30sp" >
  14. </TextView>
  15.  
  16. </LinearLayout>

5.MainActivity.java:

  1. package com.example.tabtest;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import android.app.Activity;
  7. import android.os.Bundle;
  8. import android.support.v4.view.PagerAdapter;
  9. import android.support.v4.view.ViewPager;
  10. import android.support.v4.view.ViewPager.OnPageChangeListener;
  11. import android.view.LayoutInflater;
  12. import android.view.View;
  13. import android.view.View.OnClickListener;
  14. import android.view.ViewGroup;
  15. import android.view.Window;
  16. import android.widget.ImageButton;
  17. import android.widget.LinearLayout;
  18.  
  19. public class MainActivity extends Activity implements OnClickListener {
  20. private ViewPager mViewPager;
  21. private PagerAdapter pagerAdapter;
  22. private List<View> mViews = new ArrayList<View>();
  23. private LinearLayout mLLChat;
  24. private LinearLayout mLLPengyouquan;
  25. private LinearLayout mLLAddress;
  26. private LinearLayout mLLSet;
  27. private ImageButton mImageButtonChat;
  28. private ImageButton mImageButtonPengyouquan;
  29. private ImageButton mImageButtonAddress;
  30. private ImageButton mImageButtonSet;
  31.  
  32. @Override
  33. protected void onCreate(Bundle savedInstanceState) {
  34. super.onCreate(savedInstanceState);
  35. requestWindowFeature(Window.FEATURE_NO_TITLE);
  36. setContentView(R.layout.activity_main);
  37. initView();// 初始化控件
  38. initEvent();// 点击事件
  39. }
  40.  
  41. private void initEvent() {
  42. mLLChat.setOnClickListener(this);
  43. mLLPengyouquan.setOnClickListener(this);
  44. mLLAddress.setOnClickListener(this);
  45. mLLSet.setOnClickListener(this);
  46. mViewPager.setOnPageChangeListener(new OnPageChangeListener() {//ViewPager滑动切换监听
  47.  
  48. @Override
  49. public void onPageSelected(int arg0) {
  50. int currentItem=mViewPager.getCurrentItem();
  51. resetImag();
  52. switch (currentItem) {
  53. case 0:
  54. mImageButtonChat.setImageResource(R.drawable.tab_weixin_pressed);
  55. break;
  56. case 1:
  57. mImageButtonPengyouquan.setImageResource(R.drawable.tab_find_frd_pressed);
  58. break;
  59. case 2:
  60. mImageButtonAddress.setImageResource(R.drawable.tab_address_pressed);
  61. break;
  62. case 3:
  63. mImageButtonSet.setImageResource(R.drawable.tab_settings_pressed);
  64. break;
  65.  
  66. default:
  67. break;
  68. }
  69.  
  70. }
  71.  
  72. @Override
  73. public void onPageScrolled(int arg0, float arg1, int arg2) {
  74. // TODO Auto-generated method stub
  75.  
  76. }
  77.  
  78. @Override
  79. public void onPageScrollStateChanged(int arg0) {
  80. // TODO Auto-generated method stub
  81.  
  82. }
  83. });
  84. }
  85.  
  86. private void initView() {
  87. /**
  88. * 初始化全部控件
  89. */
  90. mViewPager = (ViewPager) findViewById(R.id.viewpager);
  91. mLLChat = (LinearLayout) findViewById(R.id.ll_chat);
  92. mLLPengyouquan = (LinearLayout) findViewById(R.id.ll_pengyouquan);
  93. mLLAddress = (LinearLayout) findViewById(R.id.ll_tongxunlu);
  94. mLLSet = (LinearLayout) findViewById(R.id.ll_set);
  95. mImageButtonChat = (ImageButton) findViewById(R.id.ibtn_chat);
  96. mImageButtonPengyouquan = (ImageButton) findViewById(R.id.ibtn_pengyouquan);
  97. mImageButtonAddress = (ImageButton) findViewById(R.id.ibtn_tongxunlu);
  98. mImageButtonSet = (ImageButton) findViewById(R.id.ibtn_set);
  99. /**
  100. * 获取mViews
  101. */
  102. LayoutInflater layoutInflater = LayoutInflater.from(this);
  103. View tab01 = layoutInflater.inflate(R.layout.tab01, null);
  104. View tab02 = layoutInflater.inflate(R.layout.tab02, null);
  105. View tab03 = layoutInflater.inflate(R.layout.tab03, null);
  106. View tab04 = layoutInflater.inflate(R.layout.tab04, null);
  107. mViews.add(tab01);
  108. mViews.add(tab02);
  109. mViews.add(tab03);
  110. mViews.add(tab04);
  111. // ViewPager适配器
  112. pagerAdapter = new PagerAdapter() {
  113.  
  114. /**
  115. * 摧毁
  116. */
  117. @Override
  118. public void destroyItem(ViewGroup container, int position,
  119. Object object) {
  120. container.removeView(mViews.get(position));
  121. }
  122.  
  123. /**
  124. * 初始化
  125. */
  126.  
  127. @Override
  128. public Object instantiateItem(ViewGroup container, int position) {
  129. View view = mViews.get(position);
  130. container.addView(view);
  131. return view;
  132. }
  133.  
  134. @Override
  135. public boolean isViewFromObject(View arg0, Object arg1) {
  136. return arg0 == arg1;
  137. }
  138.  
  139. @Override
  140. public int getCount() {
  141. return mViews.size();
  142. }
  143. };
  144.  
  145. mViewPager.setAdapter(pagerAdapter);
  146.  
  147. }
  148.  
  149. @Override
  150. public void onClick(View v) {
  151. resetImag();
  152. switch (v.getId()) {
  153. case R.id.ll_chat:
  154. mViewPager.setCurrentItem(0);
  155. mImageButtonChat.setImageResource(R.drawable.tab_weixin_pressed);
  156. break;
  157. case R.id.ll_pengyouquan:
  158. mViewPager.setCurrentItem(1);
  159. mImageButtonPengyouquan.setImageResource(R.drawable.tab_find_frd_pressed);
  160.  
  161. break;
  162. case R.id.ll_tongxunlu:
  163. mViewPager.setCurrentItem(2);
  164. mImageButtonAddress.setImageResource(R.drawable.tab_address_pressed);
  165.  
  166. break;
  167. case R.id.ll_set:
  168. mViewPager.setCurrentItem(3);
  169. mImageButtonSet.setImageResource(R.drawable.tab_settings_pressed);
  170.  
  171. break;
  172.  
  173. default:
  174. break;
  175. }
  176. }
  177. /**
  178. * 将全部图片设置成未选中状态
  179. */
  180. private void resetImag() {
  181. mImageButtonChat.setImageResource(R.drawable.tab_weixin_normal);
  182. mImageButtonPengyouquan.setImageResource(R.drawable.tab_find_frd_normal);
  183. mImageButtonAddress.setImageResource(R.drawable.tab_address_normal);
  184. mImageButtonSet.setImageResource(R.drawable.tab_settings_normal);
  185. }
  186.  
  187. }

6.执行实例:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

喜欢的朋友能够关注我。谢谢

源代码下载


Android实战简易教程-第二十六枪(基于ViewPager实现微信页面切换效果)的更多相关文章

  1. Android实战简易教程-第二十八枪(Uri转String型实例)

    接上一篇文章.我们能够轻易的获取所选图片的uri,那么我们考虑怎样将获取的uri转换成String型的地址呢? 接下来我们通过实例来研究.布局文件和上篇(二十七枪)一致,我们就不再列出,直接看Main ...

  2. Android实战简易教程-第二十五枪(基于Baas的数据表查询下拉刷新和上拉载入实现!)

    上一节我们实现了数据表的载入,可是,当数据表数据非常多时.我们就要考虑数据的分页.这里我们选用了PullToRefreshListView控件,先看一下该控件的说明: 效果图:            ...

  3. Android实战简易教程-第二十八枪(基于Bmob实现头像图片设置和网络上传功能!)

    上一篇我们介绍了怎样由uri转换成String ,本文就用到了上篇文章的方法.以下我们介绍一下怎样设置头像后将头像图片上传到云端的方法,本文基于Bmob提供的服务. 看一下代码:(布局文件和前两篇文章 ...

  4. Android实战简易教程-第二十四枪(基于Baas的用户表查询功能实现!)

    接着上一篇,我们注冊了几个用户,用户表例如以下: 以下我们用ListView将表中数据显示出来吧. 首先看一下main.xml: <RelativeLayout xmlns:android=&q ...

  5. Android实战简易教程-第十五枪(实现ListView中Button点击事件监听)

    1.main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x ...

  6. Android实战简易教程-第二十三枪(基于Baas的用户注冊验证username是否反复功能!)

    接上一篇,加入验证用户名是否已经注冊功能! 仅仅须要改动MainActivity.java: package com.example.logintest; import java.util.List; ...

  7. Android实战简易教程-第十枪(画廊组件Gallery有用研究)

    Gallery组件用于拖拽浏览图片,以下我们就来看一下怎样实现. 一.实现Gallery 1.布局文件非常easy: <?xml version="1.0" encoding ...

  8. Android实战简易教程-第二十三枪(基于Baas的用户注冊和登录模块实现!)

    接着上两篇文章.我们基于Bmob提供的API实现用户登录功能.总体看一下代码. 1.注冊页面xml: <RelativeLayout xmlns:android="http://sch ...

  9. Android实战简易教程-第四十枪(窃听风云之短信监听)

    近期在做监听验证码短信自己主动填入的功能,无意间想到了一个短信监听的办法. 免责声明:短信监听本身是一种违法行为,这里仅仅是技术描写叙述.请大家学习技术就可以.(哈哈) 本实例是基于bmob提供的后台 ...

随机推荐

  1. span文本自动换行

    .span{ word-wrap: break-word; word-break: break-all; overflow: hidden; }

  2. Qt之自定义布局管理器(QFlowLayout)

    简述 QFlowLayout,顾名思义-流布局,实现了处理不同窗口大小的布局.根据应用窗口的宽度来进行控件放置的变化. 具体实现要求不再赘述,请参考前两节内容. 简述 实现 效果 源码 实现 QFlo ...

  3. 【LeetCode】Merge Intervals 题解 利用Comparator进行排序

    题目链接Merge Intervals /** * Definition for an interval. * public class Interval { * int start; * int e ...

  4. linux 下同步异步,堵塞非堵塞的一些想法

    补充: 发现一个更好的解释样例:同步是一件事我们从头到尾尾随着完毕.异步是别人完毕我们仅仅看结果. 堵塞是完毕一件事的过程中可能会遇到一些情况让我们等待(挂起).非堵塞就是发生这些情况时我们跨过. 比 ...

  5. Invalid project description.

    1.错误描写叙述 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/ ...

  6. 使用React Hook后的一些体会

    一.前言 距离React Hook发布已经有一段时间了,笔者在之前也一直在等待机会来尝试一下Hook,这个尝试不是像文档中介绍的可以先在已有项目中的小组件和新组件上尝试,而是尝试用Hook的方式构建整 ...

  7. 实习第四天(bboss框架学习)

    现在好像比较使用的管理工具是gradle管理工具,学长说这个管理工具比maven管理工具要好用! 我今天主要就是想要安装好的gradle这个管理工具,但是可能是我的eclispe版本的问题,我没能安装 ...

  8. vim-YCM插件安装

    这两天开始使用vim来学习C++语言,中间少不了要进行编译.才刚刚写了两个小例子就开始发现,每次都要退出vim来进行编译,实在太麻烦了.这时候才想到之前有在一本关于vim的书籍上看到quickfix的 ...

  9. win10安装jdk8 配置环境变量

    参考:https://jingyan.baidu.com/article/6b97984dd257b41ca2b0bf86.html  

  10. Passpoint R1

    Passpoint R1 自从 Android 6.0 支持从网络下载包含配置文件和凭据信息的特殊文件来配置 Passpoint R1(第 1 版)凭据,Android 就一直支持 Passpoint ...