Android TabLayout 使用

依赖 
compile 'com.android.support:design:25.3.1'

TabLayout属性

app:tabMode="fixed"         固定
app:tabMode="scrollable"    可滑动
app:tabIndicatorColor       滚动下划线的颜色  
app:tabSelectedTextColor    tab选中后文字的颜色  
app:tabTextColor            tab默认显示的文字颜色  
app:tabMaxWidth             tab最大宽度
app:tabMinWidth             tab最小宽度

设置tabMode为 scrollable

<android.support.design.widget.TabLayout
     android:layout_width="match_parent"
     android:layout_height="48dp"
     app:tabMode="scrollable"
     app:tabIndicatorColor="#ff00ff"
     app:tabTextColor="#000000"
     app:tabSelectedTextColor="#ff0000"
     >

添加tab标题

TabLayout mTabLayout = (TabLayout) findViewById(R.id.tabLayout);
mTabLayout.addTab(mTabLayout.newTab().setText("tab_1"));
mTabLayout.addTab(mTabLayout.newTab().setText("tab_2"));
mTabLayout.addTab(mTabLayout.newTab().setText("tab_3"));
mTabLayout.addTab(mTabLayout.newTab().setText("tab_4"));
mTabLayout.addTab(mTabLayout.newTab().setText("tab_5"));
mTabLayout.addTab(mTabLayout.newTab().setText("tab_6"));
mTabLayout.addTab(mTabLayout.newTab().setText("tab_7"));
mTabLayout.addTab(mTabLayout.newTab().setText("tab_8"));
mTabLayout.addTab(mTabLayout.newTab().setText("tab_9"));
mTabLayout.addTab(mTabLayout.newTab().setText("tab_10"));

运行后效果如下:
scrollable.gif

修改tabMode为 fixed重新运行

app:tabMode="fixed"

发现页面无法滑动。
fixed.gif

通常TabLayout和ViewPager配合在一起使用,先看图:

home.gif

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
  >
    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        app:tabMode="fixed"
        app:tabIndicatorColor="#ff00ff"
        app:tabTextColor="#000000"
        app:tabSelectedTextColor="#ff0000"
        >

    </android.support.design.widget.TabLayout>


    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v4.view.ViewPager>

</LinearLayout>

创建layout_home,layout_address,layout_discover,layout_me布局
布局都一样,只有一个TextView,分别为首页,通讯录,发现,我。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="首页"
        />
</LinearLayout>

Activity 中代码

public class TabLayoutActivity extends AppCompatActivity {

    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private List<View> mViewList;
    private List<String> mTitleList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_layout);

        mTabLayout = (TabLayout) findViewById(R.id.tabLayout);
        View home = LayoutInflater.from(getApplicationContext()).inflate(R.layout.layout_home, null);
        View address = LayoutInflater.from(getApplicationContext()).inflate(R.layout.layout_address, null);
        View discover = LayoutInflater.from(getApplicationContext()).inflate(R.layout.layout_discover, null);
        View me = LayoutInflater.from(getApplicationContext()).inflate(R.layout.layout_me, null);

        mViewList = new ArrayList<>();
        mViewList.add(home);
        mViewList.add(address);
        mViewList.add(discover);
        mViewList.add(me);

        mTitleList = new ArrayList<>();
        mTitleList.add("首页");
        mTitleList.add("通讯录");
        mTitleList.add("发现");
        mTitleList.add("我");

        mViewPager = (ViewPager) findViewById(R.id.view_pager);
        mViewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return mViewList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(mViewList.get(position));
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(mViewList.get(position));
                return mViewList.get(position);
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return mTitleList.get(position);
            }
        });

        mTabLayout.setupWithViewPager(mViewPager);

        //TabLayout监听
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //切换tab时不使用ViewPager动画效果
                mViewPager.setCurrentItem(tab.getPosition(),false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}
运行即可实现图中的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值