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提供的后台 ...
随机推荐
- 洛谷 P2633 Count on a tree 主席树
在一棵树上,我们要求点 $(u,v)$ 之间路径的第$k$大数. 对于点 $i$ ,建立 $i$ 到根节点的一棵前缀主席树. 简单容斥后不难得出结果为$sumv[u]+sumv[v]−sumv[l ...
- 在yii2.0中封装一个生成验证码的控制器
frontend目录下/封装的验证码类: <?php namespace frontend\controllers; use yii\base\Controller; class CapathC ...
- django框架-DRF视图中的request和response
1.Request 相对于django框架,DRF工程的request类则是继承自HttpRequest类,Rest framework提供了parser解释器,用来解释请求中的content_typ ...
- UVA-10003 Cutting Sticks 动态规划 找分界点k的动规
题目链接:https://cn.vjudge.net/problem/UVA-10003 题意 有根棍子,上面有些分割点(n<50),每次按分割点切割棍子时,费用为当前棍子的长度. 问有什么样的 ...
- BZOJ 3790 神奇项链(回文自动机+线段树优化DP)
我们预处理出来以i为结尾的最长回文后缀(回文自动机的构建过程中就可以求出)然后就是一个区间覆盖,因为我懒得写贪心,就写了线段树优化的DP. #include<iostream> #incl ...
- 一句话木马和中国菜刀的结合拿webshell
什么叫做一句话木马: 就是一句简单的脚本语言,一句话木马分为Php,asp,aspx等 中国菜刀: 连接一句话木马的工具 实验的目的: 通过一句话木马来控制我们的服务器,拿到webshe ...
- js img图片加载失败,重新加载+断网检查
我们常常会遇到img加载图片的时候因为网络问题或者图片过大导致图片加载失败的问题,页面就因为这张蹦掉的图变得不美观.所以我们需要图片加载失败的时候重新加载图片,前端图片加载优化 //js方法定义 fu ...
- 转:让MySQL支持emoji表情
转自:http://www.cnblogs.com/suifu/p/5848269.html 公司有新要求,ios客户端要上线评论中可以使用emoji表情的功能,在mysql 5.5 之前,UTF-8 ...
- Linux环境安装phpredis扩展
php訪问redis须要安装phpredis扩展.phpredis是用纯C语言写的. phpredis下载地址 https://github.com/phpredis/phpredis 最新的版本号是 ...
- nj11--http
概念:Node.js提供了http模块.其中封装了一个高效的HTTP服务器和一个建议的HTTP客户端. http.server是一个基于事件的HTTP服务器.内部有C++实现.接口由JavaScrip ...