Android实战简易教程-第二十六枪(基于ViewPager实现微信页面切换效果)
1.头部布局文件top.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="40dp"
- android:background="@drawable/title_bar"
- android:gravity="center"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/title"
- android:layout_width="wrap_content"
- android:layout_height="30dp"
- android:layout_gravity="center"
- android:text="WeChat"
- android:textColor="#ffffffff"
- android:textSize="25dp" />
- </LinearLayout>
2.底部布局文件bottom.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:background="@drawable/bottom_bar"
- android:orientation="horizontal" >
- <LinearLayout
- android:id="@+id/ll_chat"
- android:layout_width="0dp"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- android:gravity="center"
- android:orientation="vertical" >
- <ImageButton
- android:id="@+id/ibtn_chat"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#0000"
- android:clickable="false"
- android:src="@drawable/tab_weixin_pressed" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="聊天"
- android:textColor="#ffffffff" />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/ll_pengyouquan"
- android:layout_width="0dp"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- android:gravity="center"
- android:orientation="vertical" >
- <ImageButton
- android:id="@+id/ibtn_pengyouquan"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#0000"
- android:clickable="false"
- android:src="@drawable/tab_find_frd_normal" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="朋友圈"
- android:textColor="#ffffffff" />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/ll_tongxunlu"
- android:layout_width="0dp"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- android:gravity="center"
- android:orientation="vertical" >
- <ImageButton
- android:id="@+id/ibtn_tongxunlu"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#0000"
- android:clickable="false"
- android:src="@drawable/tab_address_normal" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="好友"
- android:textColor="#ffffffff" />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/ll_set"
- android:layout_width="0dp"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- android:gravity="center"
- android:orientation="vertical" >
- <ImageButton
- android:id="@+id/ibtn_set"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#0000"
- android:clickable="false"
- android:src="@drawable/tab_settings_normal" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="设置"
- android:textColor="#ffffffff" />
- </LinearLayout>
- </LinearLayout>
3.主布局文件:
- <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" >
- <include layout="@layout/top" />
- <android.support.v4.view.ViewPager
- android:id="@+id/viewpager"
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="1" >
- </android.support.v4.view.ViewPager>
- <include layout="@layout/bottom" />
- </LinearLayout>
4.四个ViewPager的内容页布局
tab01.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:gravity="center"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:text="聊天页"
- android:textSize="30sp" >
- </TextView>
- </LinearLayout>
tab02.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:gravity="center"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:text="朋友圈"
- android:textSize="30sp" >
- </TextView>
- </LinearLayout>
tab03.xml:
- <?
- xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:gravity="center"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:text="联系人页面"
- android:textSize="30sp" >
- </TextView>
- </LinearLayout>
tab04.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:gravity="center"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:text="设置页面"
- android:textSize="30sp" >
- </TextView>
- </LinearLayout>
5.MainActivity.java:
- package com.example.tabtest;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.os.Bundle;
- import android.support.v4.view.PagerAdapter;
- import android.support.v4.view.ViewPager;
- import android.support.v4.view.ViewPager.OnPageChangeListener;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup;
- import android.view.Window;
- import android.widget.ImageButton;
- import android.widget.LinearLayout;
- public class MainActivity extends Activity implements OnClickListener {
- private ViewPager mViewPager;
- private PagerAdapter pagerAdapter;
- private List<View> mViews = new ArrayList<View>();
- private LinearLayout mLLChat;
- private LinearLayout mLLPengyouquan;
- private LinearLayout mLLAddress;
- private LinearLayout mLLSet;
- private ImageButton mImageButtonChat;
- private ImageButton mImageButtonPengyouquan;
- private ImageButton mImageButtonAddress;
- private ImageButton mImageButtonSet;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.activity_main);
- initView();// 初始化控件
- initEvent();// 点击事件
- }
- private void initEvent() {
- mLLChat.setOnClickListener(this);
- mLLPengyouquan.setOnClickListener(this);
- mLLAddress.setOnClickListener(this);
- mLLSet.setOnClickListener(this);
- mViewPager.setOnPageChangeListener(new OnPageChangeListener() {//ViewPager滑动切换监听
- @Override
- public void onPageSelected(int arg0) {
- int currentItem=mViewPager.getCurrentItem();
- resetImag();
- switch (currentItem) {
- case 0:
- mImageButtonChat.setImageResource(R.drawable.tab_weixin_pressed);
- break;
- case 1:
- mImageButtonPengyouquan.setImageResource(R.drawable.tab_find_frd_pressed);
- break;
- case 2:
- mImageButtonAddress.setImageResource(R.drawable.tab_address_pressed);
- break;
- case 3:
- mImageButtonSet.setImageResource(R.drawable.tab_settings_pressed);
- break;
- default:
- break;
- }
- }
- @Override
- public void onPageScrolled(int arg0, float arg1, int arg2) {
- // TODO Auto-generated method stub
- }
- @Override
- public void onPageScrollStateChanged(int arg0) {
- // TODO Auto-generated method stub
- }
- });
- }
- private void initView() {
- /**
- * 初始化全部控件
- */
- mViewPager = (ViewPager) findViewById(R.id.viewpager);
- mLLChat = (LinearLayout) findViewById(R.id.ll_chat);
- mLLPengyouquan = (LinearLayout) findViewById(R.id.ll_pengyouquan);
- mLLAddress = (LinearLayout) findViewById(R.id.ll_tongxunlu);
- mLLSet = (LinearLayout) findViewById(R.id.ll_set);
- mImageButtonChat = (ImageButton) findViewById(R.id.ibtn_chat);
- mImageButtonPengyouquan = (ImageButton) findViewById(R.id.ibtn_pengyouquan);
- mImageButtonAddress = (ImageButton) findViewById(R.id.ibtn_tongxunlu);
- mImageButtonSet = (ImageButton) findViewById(R.id.ibtn_set);
- /**
- * 获取mViews
- */
- LayoutInflater layoutInflater = LayoutInflater.from(this);
- View tab01 = layoutInflater.inflate(R.layout.tab01, null);
- View tab02 = layoutInflater.inflate(R.layout.tab02, null);
- View tab03 = layoutInflater.inflate(R.layout.tab03, null);
- View tab04 = layoutInflater.inflate(R.layout.tab04, null);
- mViews.add(tab01);
- mViews.add(tab02);
- mViews.add(tab03);
- mViews.add(tab04);
- // ViewPager适配器
- pagerAdapter = new PagerAdapter() {
- /**
- * 摧毁
- */
- @Override
- public void destroyItem(ViewGroup container, int position,
- Object object) {
- container.removeView(mViews.get(position));
- }
- /**
- * 初始化
- */
- @Override
- public Object instantiateItem(ViewGroup container, int position) {
- View view = mViews.get(position);
- container.addView(view);
- return view;
- }
- @Override
- public boolean isViewFromObject(View arg0, Object arg1) {
- return arg0 == arg1;
- }
- @Override
- public int getCount() {
- return mViews.size();
- }
- };
- mViewPager.setAdapter(pagerAdapter);
- }
- @Override
- public void onClick(View v) {
- resetImag();
- switch (v.getId()) {
- case R.id.ll_chat:
- mViewPager.setCurrentItem(0);
- mImageButtonChat.setImageResource(R.drawable.tab_weixin_pressed);
- break;
- case R.id.ll_pengyouquan:
- mViewPager.setCurrentItem(1);
- mImageButtonPengyouquan.setImageResource(R.drawable.tab_find_frd_pressed);
- break;
- case R.id.ll_tongxunlu:
- mViewPager.setCurrentItem(2);
- mImageButtonAddress.setImageResource(R.drawable.tab_address_pressed);
- break;
- case R.id.ll_set:
- mViewPager.setCurrentItem(3);
- mImageButtonSet.setImageResource(R.drawable.tab_settings_pressed);
- break;
- default:
- break;
- }
- }
- /**
- * 将全部图片设置成未选中状态
- */
- private void resetImag() {
- mImageButtonChat.setImageResource(R.drawable.tab_weixin_normal);
- mImageButtonPengyouquan.setImageResource(R.drawable.tab_find_frd_normal);
- mImageButtonAddress.setImageResource(R.drawable.tab_address_normal);
- mImageButtonSet.setImageResource(R.drawable.tab_settings_normal);
- }
- }
6.执行实例:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
喜欢的朋友能够关注我。谢谢
Android实战简易教程-第二十六枪(基于ViewPager实现微信页面切换效果)的更多相关文章
- Android实战简易教程-第二十八枪(Uri转String型实例)
接上一篇文章.我们能够轻易的获取所选图片的uri,那么我们考虑怎样将获取的uri转换成String型的地址呢? 接下来我们通过实例来研究.布局文件和上篇(二十七枪)一致,我们就不再列出,直接看Main ...
- Android实战简易教程-第二十五枪(基于Baas的数据表查询下拉刷新和上拉载入实现!)
上一节我们实现了数据表的载入,可是,当数据表数据非常多时.我们就要考虑数据的分页.这里我们选用了PullToRefreshListView控件,先看一下该控件的说明: 效果图: ...
- Android实战简易教程-第二十八枪(基于Bmob实现头像图片设置和网络上传功能!)
上一篇我们介绍了怎样由uri转换成String ,本文就用到了上篇文章的方法.以下我们介绍一下怎样设置头像后将头像图片上传到云端的方法,本文基于Bmob提供的服务. 看一下代码:(布局文件和前两篇文章 ...
- Android实战简易教程-第二十四枪(基于Baas的用户表查询功能实现!)
接着上一篇,我们注冊了几个用户,用户表例如以下: 以下我们用ListView将表中数据显示出来吧. 首先看一下main.xml: <RelativeLayout xmlns:android=&q ...
- Android实战简易教程-第十五枪(实现ListView中Button点击事件监听)
1.main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x ...
- Android实战简易教程-第二十三枪(基于Baas的用户注冊验证username是否反复功能!)
接上一篇,加入验证用户名是否已经注冊功能! 仅仅须要改动MainActivity.java: package com.example.logintest; import java.util.List; ...
- Android实战简易教程-第十枪(画廊组件Gallery有用研究)
Gallery组件用于拖拽浏览图片,以下我们就来看一下怎样实现. 一.实现Gallery 1.布局文件非常easy: <?xml version="1.0" encoding ...
- Android实战简易教程-第二十三枪(基于Baas的用户注冊和登录模块实现!)
接着上两篇文章.我们基于Bmob提供的API实现用户登录功能.总体看一下代码. 1.注冊页面xml: <RelativeLayout xmlns:android="http://sch ...
- Android实战简易教程-第四十枪(窃听风云之短信监听)
近期在做监听验证码短信自己主动填入的功能,无意间想到了一个短信监听的办法. 免责声明:短信监听本身是一种违法行为,这里仅仅是技术描写叙述.请大家学习技术就可以.(哈哈) 本实例是基于bmob提供的后台 ...
随机推荐
- span文本自动换行
.span{ word-wrap: break-word; word-break: break-all; overflow: hidden; }
- Qt之自定义布局管理器(QFlowLayout)
简述 QFlowLayout,顾名思义-流布局,实现了处理不同窗口大小的布局.根据应用窗口的宽度来进行控件放置的变化. 具体实现要求不再赘述,请参考前两节内容. 简述 实现 效果 源码 实现 QFlo ...
- 【LeetCode】Merge Intervals 题解 利用Comparator进行排序
题目链接Merge Intervals /** * Definition for an interval. * public class Interval { * int start; * int e ...
- linux 下同步异步,堵塞非堵塞的一些想法
补充: 发现一个更好的解释样例:同步是一件事我们从头到尾尾随着完毕.异步是别人完毕我们仅仅看结果. 堵塞是完毕一件事的过程中可能会遇到一些情况让我们等待(挂起).非堵塞就是发生这些情况时我们跨过. 比 ...
- Invalid project description.
1.错误描写叙述 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/ ...
- 使用React Hook后的一些体会
一.前言 距离React Hook发布已经有一段时间了,笔者在之前也一直在等待机会来尝试一下Hook,这个尝试不是像文档中介绍的可以先在已有项目中的小组件和新组件上尝试,而是尝试用Hook的方式构建整 ...
- 实习第四天(bboss框架学习)
现在好像比较使用的管理工具是gradle管理工具,学长说这个管理工具比maven管理工具要好用! 我今天主要就是想要安装好的gradle这个管理工具,但是可能是我的eclispe版本的问题,我没能安装 ...
- vim-YCM插件安装
这两天开始使用vim来学习C++语言,中间少不了要进行编译.才刚刚写了两个小例子就开始发现,每次都要退出vim来进行编译,实在太麻烦了.这时候才想到之前有在一本关于vim的书籍上看到quickfix的 ...
- win10安装jdk8 配置环境变量
参考:https://jingyan.baidu.com/article/6b97984dd257b41ca2b0bf86.html
- Passpoint R1
Passpoint R1 自从 Android 6.0 支持从网络下载包含配置文件和凭据信息的特殊文件来配置 Passpoint R1(第 1 版)凭据,Android 就一直支持 Passpoint ...