MainActivity
package com.example.ziwang.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity implements View.OnClickListener {
private ImageButton tab_weixin_img;
private ImageButton tab_address_img;
private ImageButton tab_find_frd_img;
private ImageButton tab_setting_img;
private LinearLayout tab_weixin;
private LinearLayout tab_address;
private LinearLayout tab_find_frd;
private LinearLayout tab_setting;
private ViewPager viewPager;
private PagerAdapter pagerApapter;
private List<View> l1 = new ArrayList<View>();
private LayoutInflater my;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initview();
}
private void initview() {
viewPager = (ViewPager) findViewById(R.id.id_viewpager);
tab_weixin_img = (ImageButton) findViewById(R.id.tab_weixin_img);
tab_address_img = (ImageButton) findViewById(R.id.tab_address_img);
tab_find_frd_img = (ImageButton) findViewById(R.id.tab_frd_img);
tab_setting_img = (ImageButton) findViewById(R.id.tab_setting_img);
tab_weixin = (LinearLayout) findViewById(R.id.tab_weixin);
tab_address = (LinearLayout) findViewById(R.id.tab_address);
tab_find_frd = (LinearLayout) findViewById(R.id.tab_frd);
tab_setting = (LinearLayout) findViewById(R.id.tab_setting);
tab_weixin.setOnClickListener(this);
tab_address.setOnClickListener(this);
tab_find_frd.setOnClickListener(this);
tab_setting.setOnClickListener(this);
my = LayoutInflater.from(this);
View tab0 = my.inflate(R.layout.tab0,null);
View tab1 = my.inflate(R.layout.tab1,null);
View tab2 = my.inflate(R.layout.tab2,null);
View tab3 = my.inflate(R.layout.tab3,null);
l1.add(tab0);l1.add(tab1);l1.add(tab2);l1.add(tab3);
pagerApapter = new PagerAdapter() {
@Override
public int getCount() {
return l1.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(l1.get(position));
return l1.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(l1.get(position));
}
};
viewPager.setAdapter(pagerApapter);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener(){
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
reset();
int currentItem = viewPager.getCurrentItem();
switch (currentItem){
case 0:
tab_weixin_img.setImageResource(R.drawable.tab_weixin_pressed);
break;
case 1:
tab_address_img.setImageResource(R.drawable.tab_address_pressed);
break;
case 2:
tab_find_frd_img.setImageResource(R.drawable.tab_find_frd_pressed);
break;
case 3:
tab_setting_img.setImageResource(R.drawable.tab_settings_pressed);
break;
default:
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
@Override
public void onClick(View view) {
reset();
switch (view.getId()){
case R.id.tab_weixin:
tab_weixin_img.setImageResource(R.drawable.tab_weixin_pressed);
viewPager.setCurrentItem(0);
break;
case R.id.tab_address:
tab_address_img.setImageResource(R.drawable.tab_address_pressed);
viewPager.setCurrentItem(1);
break;
case R.id.tab_frd:
tab_find_frd_img.setImageResource(R.drawable.tab_find_frd_pressed);
viewPager.setCurrentItem(2);
break;
case R.id.tab_setting:
tab_setting_img.setImageResource(R.drawable.tab_settings_pressed);
viewPager.setCurrentItem(3);
break;
default:
break;
}
}
private void reset() {
tab_weixin_img.setImageResource(R.drawable.tab_weixin_normal);
tab_address_img.setImageResource(R.drawable.tab_address_normal);
tab_find_frd_img.setImageResource(R.drawable.tab_find_frd_normal);
tab_setting_img.setImageResource(R.drawable.tab_settings_normal);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.ziwang.myapplication.MainActivity"
android:orientation="vertical"
>
<include layout="@layout/top"/>
<android.support.v4.view.ViewPager
android:id="@+id/id_viewpager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</android.support.v4.view.ViewPager>
<include layout="@layout/buttom"/>
</LinearLayout>
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="55dp"
android:background="#000000">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="微信"
android:textColor="#ffffff"
android:textSize="22dp"
android:textStyle="bold" />
</LinearLayout>
<?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="70dp">
<LinearLayout
android:id="@+id/tab_weixin"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:orientation="vertical"
android:background="#000000">
<ImageButton
android:id="@+id/tab_weixin_img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/tab_weixin_normal"
android:clickable="false"
android:background="#000000"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="微信"
android:textColor="#ffffff"
android:textSize="20dp"
android:gravity="center"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/tab_address"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:orientation="vertical"
android:background="#000000">
<ImageButton
android:id="@+id/tab_address_img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/tab_address_normal"
android:clickable="false"
android:background="#000000"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="通讯录"
android:textColor="#ffffff"
android:textSize="20dp"
android:gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:orientation="vertical"
android:background="#000000"
android:id="@+id/tab_frd">
<ImageButton
android:id="@+id/tab_frd_img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/tab_find_frd_normal"
android:clickable="false"
android:background="#000000"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="朋友"
android:textColor="#ffffff"
android:textSize="20dp"
android:gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:orientation="vertical"
android:background="#000000"
android:id="@+id/tab_setting">
<ImageButton
android:id="@+id/tab_setting_img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/tab_settings_normal"
android:clickable="false"
android:background="#000000"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="设置"
android:textColor="#ffffff"
android:textSize="20dp"
android:gravity="center"
/>
</LinearLayout>
</LinearLayout>
tab0-tab3
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="微信界面"
android:gravity="center"
android:textSize="35dp"
/>
</LinearLayout>