TabLayout 簡單使用。


先上效果圖

 

在使用TabLayout 之前需要導入design包。 我使用的是android studio 只要在build.gradle中加入

compile 'com.android.support:design:24.2.0' 即可。

一、首先看一下布局文件
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     xmlns:app="http://schemas.android.com/apk/res-auto"
 5     xmlns:tools="http://schemas.android.com/tools"
 6     android:layout_width="match_parent"
 7     android:layout_height="match_parent"
 8     android:orientation="vertical"
 9     tools:context=".MainActivity">
10 
11 
12     <android.support.design.widget.TabLayout
13         android:id="@+id/tablayout"
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:background="@color/colorPrimary"
17         app:tabIndicatorColor="@color/white"
18         app:tabSelectedTextColor="@color/red"
19         app:tabTextColor="@color/white"
20         >
21     </android.support.design.widget.TabLayout  >
22 
23     <android.support.v4.view.ViewPager
24         android:id="@+id/viewpage"
25         android:layout_width="match_parent"
26         android:layout_height="0dp"
27         android:layout_weight="1">
28 
29     </android.support.v4.view.ViewPager>
30 
31 </LinearLayout>
View Code

布局很簡單,一個是TabLayout用來顯示文字的,一個是ViewPage 用來真是頁面內容的

其中
  app:tabIndicatorColor="@color/white"     // 選中項下面的標線顏色
  app:tabSelectedTextColor="@color/red"    // 選中時標題字體顏色
  app:tabTextColor="@color/white"        // 標題字體默認顏色

 

二、 我們看看 TabLayout 的適配器,很簡單

package lyf.com.tablayoutdemo;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;

/**
 * lyf on 2016/9/24.
 * Fragment 適配器
 */
public class TabAdapter extends FragmentPagerAdapter {

    //Fragment集合
    private List<Fragment> listFragment;
    //Tab名稱集合
    private List<String> listTitle;

    public TabAdapter(FragmentManager fm,List<Fragment> listF,List<String> listS) {
        super(fm);
        listFragment = listF;
        listTitle = listS;
    }

    @Override
    public Fragment getItem(int position) {
        return listFragment.get(position);
    }

    @Override
    public int getCount() {
        return listFragment.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return listTitle.get(position);
    }
}
View Code

 

三、看看我們的主布局界面

package lyf.com.tablayoutdemo;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

import java.util.ArrayList;
import java.util.List;


/**
 * FragmentActivity 應該導入 android.support.v4.app.Fragment包
 */
public class MainActivity extends FragmentActivity {

    private List<Fragment> fragmentList = new ArrayList<>();
    private List<String> titleList = new ArrayList<>();

    private TabLayout tabLayout;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabLayout = (TabLayout) this.findViewById(R.id.tablayout);
        viewPager = (ViewPager) this.findViewById(R.id.viewpage);
        initControls();
    }

    private void initControls() {

        fragmentList.add(new OneFragment());
        fragmentList.add(new TwoFragment());
        fragmentList.add(new ThreeFragment());
        fragmentList.add(new FourFragment());

        titleList.add("推薦");
        titleList.add("視頻");
        titleList.add("熱點");
        titleList.add("娛樂");

        //添加標簽選項布局樣式
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

        tabLayout.addTab(tabLayout.newTab().setText(titleList.get(0)));
        tabLayout.addTab(tabLayout.newTab().setText(titleList.get(1)));
        tabLayout.addTab(tabLayout.newTab().setText(titleList.get(2)));
        tabLayout.addTab(tabLayout.newTab().setText(titleList.get(3)));

        //設置頁面選項
        TabAdapter tabAdapter = new TabAdapter(this.getSupportFragmentManager(), fragmentList, titleList);
        viewPager.setAdapter(tabAdapter);
        tabLayout.setupWithViewPager(viewPager);
    }
}
View Code

這里面的Fragment 就不貼出來了,大家可以用來展示自己的Fragment。這樣我們的Demo 就結束了。

 

注意:包的引用 ViewPage和Fragment 都是引用 support.V4.app包里的。

 

 
        








免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM