在Android開發中,經常要使用頂部選單,如網易新聞、今日頭條等,實現頂部選單以前是ViewPager配合PagerTabStrip來實現,但Android 5.0以後,推薦大家使用MD中的TabLayout來實現,很簡單哦~~
開發環境:Android Studio
開發步驟
- 1、在 build.gradle 匯入 com.android.support:design:22.2.1
- 2、Activity佈局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dp" //選單高度
android:background="#1FBCD2" //選單條的背景
app:tabIndicatorColor="#ff0000" //選單指示器的顏色
app:tabMode="scrollable" //選單可以滾動
app:tabPaddingEnd="20dp"
app:tabPaddingStart="20dp" //選單之間的間距
app:tabSelectedTextColor="#FFFFFF" //選單選中時的顏色
app:tabTextColor="#000000"> //選單未選中時的顏色
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
複製程式碼
- 3、Activity程式碼
public class MainActivity extends Activity {
private TabLayout mTabLayout;
private ViewPager mViewPager;
private LayoutInflater mInflater;
private List<String> mTitleList = new ArrayList<String>();//頁卡標題集合
private View view1, view2, view3, view4, view5, view6, view7, view8;//頁卡檢視
private List<View> mViewList = new ArrayList<>();//頁卡檢視集合
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.vp_view);
mTabLayout = (TabLayout) findViewById(R.id.tabs);
mInflater = LayoutInflater.from(this);
view1 = mInflater.inflate(R.layout.content, null);
view2 = mInflater.inflate(R.layout.content, null);
view3 = mInflater.inflate(R.layout.content, null);
view4 = mInflater.inflate(R.layout.content, null);
view5 = mInflater.inflate(R.layout.content, null);
view6 = mInflater.inflate(R.layout.content, null);
view7 = mInflater.inflate(R.layout.content, null);
view8 = mInflater.inflate(R.layout.content, null);
//新增頁卡檢視
mViewList.add(view1);
mViewList.add(view2);
mViewList.add(view3);
mViewList.add(view4);
mViewList.add(view5);
mViewList.add(view6);
mViewList.add(view7);
mViewList.add(view8);
//新增頁卡標題
mTitleList.add("頭條");
mTitleList.add("熱點");
mTitleList.add("本地");
mTitleList.add("財經");
mTitleList.add("科技");
mTitleList.add("教育");
mTitleList.add("體育");
mTitleList.add("笑話");
//新增tab選項卡,預設第一個選中
mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(0)), true);
mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(1)));
mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(2)));
mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(3)));
mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(4)));
mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(5)));
mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(6)));
mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(7)));
MyPagerAdapter mAdapter = new MyPagerAdapter(mViewList);
//給ViewPager設定介面卡
mViewPager.setAdapter(mAdapter);
//將TabLayout和ViewPager關聯起來
mTabLayout.setupWithViewPager(mViewPager);
//給Tabs設定介面卡
mTabLayout.setTabsFromPagerAdapter(mAdapter);
}
//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);//頁卡標題
}
}
}
複製程式碼
- 4、實現效果如下
- 5、注意點
如果真的按上面一字不差走下來的話,會發現TabLayout在ViewPager滑動的時候會出現選單文字閃爍的現象。這個應該是版本的bug,想解決的話使用22.0裡沒有bug的版本(compile 'com.android.support:design:22.2.0')或者升級到23.x