今天在慕课上学了仿微信的滑动,于是就重新敲了代码在原有的图形上又增加了改变字体的颜色。这里将代码放在这里便于以后学习。
整个过程用了ViewPager与PagerAdapter,思路捋清了还是挺简单的。
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="45dp"
android:background="@drawable/title_bar"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="微信"
android:textColor="#ffffff"
android:textSize="25sp" />
</LinearLayout>
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="55dp"
android:background="@drawable/bottom_bar"
android:baselineAligned="false"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/tab_weixin"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/bg_img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/tab_weixin_pressed" />
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="微信"
android:textColor="@color/mycolor"
android:textSize="12sp" />
</LinearLayout>
<!-- 第2部分 -->
<LinearLayout
android:id="@+id/tab_address"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/bg_img2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/tab_address_normal" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="通讯录"
android:textColor="#ffffff"
android:textSize="12sp" />
</LinearLayout>
<!-- 第3个图标部分 -->
<LinearLayout
android:id="@+id/tab_friend"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/bg_img3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/tab_find_frd_normal" />
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="朋友圈"
android:textColor="#ffffff"
android:textSize="12sp" />
</LinearLayout>
<!-- 第4部分 -->
<LinearLayout
android:id="@+id/tab_setting"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/bg_img4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/tab_settings_normal" />
<TextView
android:id="@+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置"
android:textColor="#ffffff"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
activity_main.xml
<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/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</android.support.v4.view.ViewPager>
<include layout="@layout/bottom" />
</LinearLayout>
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:orientation="vertical" >
<TextView
android:id="@+id/tab01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="微信"
android:textSize="25sp" />
</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:orientation="vertical" >
<TextView
android:id="@+id/tab02"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="通讯录"
android:textSize="25sp" />
</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:orientation="vertical" >
<TextView
android:id="@+id/tab03"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="朋友圈"
android:textSize="25sp" />
</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:orientation="vertical" >
<TextView
android:id="@+id/tab04"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="设置"
android:textSize="25sp" />
</LinearLayout>
颜色
<color name="mycolor">#558B2F</color>
MainActivity.java
package com.example.weixintest01;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.Color;
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.ViewGroup;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private ViewPager mPager;
List<View> mlist = new ArrayList<View>();
private LinearLayout mLayout1;
private LinearLayout mLayout2;
private LinearLayout mLayout3;
private LinearLayout mLayout4;
private ImageView img1;
private ImageView img2;
private ImageView img3;
private ImageView img4;
private TextView tv1;
private TextView tv2;
private TextView tv3;
private TextView tv4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
init();
initEvent();
}
private void initEvent() {
mLayout1.setOnClickListener(this);
mLayout2.setOnClickListener(this);
mLayout3.setOnClickListener(this);
mLayout4.setOnClickListener(this);
mPager.setAdapter(adapter);//设置适配器
mPager.setOnPageChangeListener(new OnPageChangeListener() {//设置页面滑动监听器
@Override
public void onPageSelected(int arg0) {
int current = mPager.getCurrentItem();
resetImage();
switch (current) {
case 0:
//设置按下去的图片和字体颜色 img1.setImageResource(R.drawable.tab_weixin_pressed);
tv1.setTextColor(Color.GREEN);
break;
case 1:
img2.setImageResource(R.drawable.tab_address_pressed);
tv2.setTextColor(Color.GREEN);
break;
case 2:
img3.setImageResource(R.drawable.tab_find_frd_pressed);
tv3.setTextColor(Color.GREEN);
break;
case 3:
img4.setImageResource(R.drawable.tab_settings_pressed);
tv4.setTextColor(Color.GREEN);
break;
default:
break;
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
private void init() {
// 实例化ViewPager
mPager = (ViewPager) findViewById(R.id.content);
// 实例化bottom布局
mLayout1 = (LinearLayout) findViewById(R.id.tab_weixin);
mLayout2 = (LinearLayout) findViewById(R.id.tab_address);
mLayout3 = (LinearLayout) findViewById(R.id.tab_friend);
mLayout4 = (LinearLayout) findViewById(R.id.tab_setting);
// 实例化bottom的imageView
img1 = (ImageView) findViewById(R.id.bg_img1);
img2 = (ImageView) findViewById(R.id.bg_img2);
img3 = (ImageView) findViewById(R.id.bg_img3);
img4 = (ImageView) findViewById(R.id.bg_img4);
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
tv3 = (TextView) findViewById(R.id.tv3);
tv4 = (TextView) findViewById(R.id.tv4);
// 加载布局
LayoutInflater inflater = LayoutInflater.from(this);
View tab01 = inflater.inflate(R.layout.tab01, null);
View tab02 = inflater.inflate(R.layout.tab02, null);
View tab03 = inflater.inflate(R.layout.tab03, null);
View tab04 = inflater.inflate(R.layout.tab04, null);
// 添加到容器中
mlist.add(tab01);
mlist.add(tab02);
mlist.add(tab03);
mlist.add(tab04);
}
PagerAdapter adapter = new PagerAdapter() {
// 初始化布局
@Override
public Object instantiateItem(ViewGroup container, int position) {
//添加当前位置的布局
container.addView(mlist.get(position));
return mlist.get(position);
}
// 移除当前位置的布局
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(mlist.get(position));
}
// 获得容器中的个数
@Override
public int getCount() {
return mlist.size();
}
// 判断View==object
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
};
// 布局的点击事件
@Override
public void onClick(View v) {
resetImage();//前提将所有的图片设置为暗色,字体颜色设置为白色,然后在下面的点击事件中重新设置图片和字体颜色
switch (v.getId()) {
case R.id.tab_weixin:
mPager.setCurrentItem(0);
img1.setImageResource(R.drawable.tab_weixin_pressed);
tv1.setTextColor(Color.GREEN);
break;
case R.id.tab_address:
mPager.setCurrentItem(1);
img2.setImageResource(R.drawable.tab_address_pressed);
tv2.setTextColor(Color.GREEN);
break;
case R.id.tab_friend:
mPager.setCurrentItem(2);
img3.setImageResource(R.drawable.tab_find_frd_pressed);
tv3.setTextColor(Color.GREEN);
break;
case R.id.tab_setting:
mPager.setCurrentItem(3);
img4.setImageResource(R.drawable.tab_settings_pressed);
tv4.setTextColor(Color.GREEN);
break;
default:
break;
}
}
//设置图片和字体的颜色
private void resetImage() {
img1.setImageResource(R.drawable.tab_weixin_normal);
img2.setImageResource(R.drawable.tab_address_normal);
img3.setImageResource(R.drawable.tab_find_frd_normal);
img4.setImageResource(R.drawable.tab_settings_normal);
tv1.setTextColor(Color.WHITE);
tv2.setTextColor(Color.WHITE);
tv3.setTextColor(Color.WHITE);
tv4.setTextColor(Color.WHITE);
}
}