———-方法一:———-
效果图:
需要的组件:
ViewPager+PagerTabStrip
布局文件代码:
<!--xmlns:android_custom="http://schemas.android.com/apk/res/com.pengkv.bigo"-->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
>
<android.support.v4.view.ViewPager
android:id="@+id/vp_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.PagerTabStrip
android:id="@+id/pts_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.PagerTabStrip>
</android.support.v4.view.ViewPager>
</RelativeLayout>
页面代码:
package com.pengkv.bigo;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
/**
* Created by peng on 2015/6/10.
*/
public class ThreeActivity extends Activity {
private ViewPager mViewPager;
private PagerTabStrip mTabStrip;//一个viewpager的指示器,效果就是一个横的粗的下划线
private LayoutInflater mInflater;
private List<String> mTitleList = new ArrayList<>();//页卡标题集合
private View view1, view2, view3, view4, view5;//页卡视图
private List<View> mViewList = new ArrayList<>();//页卡视图集合
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_three);
mViewPager = (ViewPager) findViewById(R.id.vp_view);
//对分页条进行设置
mTabStrip = (PagerTabStrip) findViewById(R.id.pts_view);
mTabStrip.setDrawFullUnderline(true); //设置Tab是否显示下划线
mTabStrip.setTabIndicatorColor(Color.MAGENTA); //设置Tab选中时的颜色
mTabStrip.setBackgroundColor(Color.DKGRAY); //设置Tab背景色
mTabStrip.setTextColor(Color.WHITE);//设置字体颜色
mInflater = LayoutInflater.from(this);
view1 = mInflater.inflate(R.layout.activity_main, null);
view2 = mInflater.inflate(R.layout.activity_main, null);
view3 = mInflater.inflate(R.layout.activity_main, null);
view4 = mInflater.inflate(R.layout.activity_main, null);
view5 = mInflater.inflate(R.layout.activity_main, null);
//添加页卡视图
mViewList.add(view1);
mViewList.add(view2);
mViewList.add(view3);
mViewList.add(view4);
mViewList.add(view5);
//添加页卡标题
mTitleList.add("No:1");
mTitleList.add("No:2");
mTitleList.add("No:3");
mTitleList.add("No:4");
mTitleList.add("No:5");
mViewPager.setAdapter(new MyPagerAdapter(mViewList));
}
//ViewPager适配器
class MyPagerAdapter extends PagerAdapter {
private List<View> mViewList;
public MyPagerAdapter(List<View> mViewList) {
this.mViewList = mViewList;
}
@Override
public int getCount() {
return mViewList.size();//页卡数
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;//官方推荐写法
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(mViewList.get(position));//添加页卡
return mViewList.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(mViewList.get(position));//删除页卡
}
@Override
public CharSequence getPageTitle(int position) {
return mTitleList.get(position);//页卡标题
}
}
}
———-方法二———-:
效果图:
需要的组件:
ViewPager+SlidingTabLayout+SlidingTabStrip(源码见附录)
布局代码:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<com.pengkv.bigo.SlidingTabLayout
android:id="@+id/stl_square"
android:layout_width=