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提供的后台 ...
随机推荐
- Python开发的简单记事本
---恢复内容开始--- 主要是利用python 自带的tkinter 库 程序的基于python3.0以上 ,各个平台都可以使用包括linux ,windows , ...
- Java Class文件结构
此文件格式为JAVA7的格式,可能与JAVA6 CLASS不一致. 每一个Class都对应着唯一的一个类或借口的定义信息.这里,我们称为"Class文件格式"只是通俗的将任意一个符 ...
- vi 学习记录
i 光标所在前插入 I 光标所在行的第一个非空字符前进入输入模式 a 光标所在后插入 A 光标所在最后插入 o 光标所在列下新增一列并进入输入模式 O 光标所在列上新增一列并进入输入模式 退出 :q, ...
- 仿百度排列图片预览插件-Simple Lightbox
很久以前遇到过这样的一个面试题,要求手写代码,实现百度图片的排列预览,并且可以左右点击查看下一张照片,当时没有做出来,这个问题也就一直放在了脑后,工作之后,遇到这样的需求之后,第一反应想到的是在源码网 ...
- EEPlat PaaS中的多租户数据隔离模式
EEPlat PaaS支持三种租户的数据隔离技术:Sparce Column.tenantId字段隔离.每一个租户独立数据库. 1)Sparce Column,和Salesforce Appforce ...
- JavaScript中的Array对象方法调用
方法concat for 循环与for in 循环 <html> <head> <script type="text/javascript"> ...
- less02-变量
html <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...
- poj--1904--King's Quest(scc建图)
King's Quest Time Limit: 15000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submit ...
- Flume框架基础
* Flume框架基础 框架简介: ** Flume提供一个分布式的,可靠的,对大数据量的日志进行高效收集.聚集.移动的服务,Flume只能在Unix环境下运行. ** Flume基于流式架构,容错性 ...
- Andoid 更好的Android多线程下载框架
概述 为什么是更好的Android多线程下载框架呢,原因你懂的,广告法嘛! 本篇我们我们就来聊聊多线程下载框架,先聊聊我们框架的特点: 多线程 多任务 断点续传 支持大文件 可以自定义下载数据库 高度 ...