android TabLayout的简单使用

布局文件

<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/tab_home_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/titleBlue"
        app:tabIndicatorColor="@color/red"
        app:tabSelectedTextColor="@color/gray"
        app:tabTextColor="@color/white"
        />
    <android.support.v4.view.ViewPager
        android:id="@+id/vp_home_pager"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />

</LinearLayout>
<!--    app:tabIndicatorColor="@color/white"                 // 下方滚动的下划线颜色
    app:tabSelectedTextColor="@color/gray"               // tab被选中后,文字的颜色
    app:tabTextColor="@color/white"                      // tab默认的文字颜色
    app:tabIndicatorHeight="2dp"                        //下方滚动的下划线的高度
    -->

主代码:

public class HomeFragment extends Fragment {

    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private List<Fragment> mFragments;
    private String[] mTitles;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        View view=LayoutInflater.from(getContext()).inflate(R.layout.fragment_home,null);
        mTabLayout = (TabLayout) view.findViewById(R.id.tab_home_title);
        mViewPager = (ViewPager) view.findViewById(R.id.vp_home_pager);

        initData();

        return view;
    }

    private void initData() {
        mFragments = new ArrayList<>();
        mFragments.add(new NewFragment());
        mFragments.add(new PicFragment());
        mFragments.add(new FootFragment());
        mFragments.add(new GirlsFragment());
        mFragments.add(new SportsFragment());
        mFragments.add(new PeopleFragment());

        mTitles = new String[]{"新闻","图片","美食","美女","体育","人物"};

        mViewPager.setAdapter(new MyPagerAdapter(getFragmentManager()));
        //将TabLayout和ViewPager关联起来。
        mTabLayout.setupWithViewPager(mViewPager);
        //设置可以滑动
        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

    }

    class MyPagerAdapter extends FragmentPagerAdapter{


        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            return mFragments.get(position);
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }
    }
}

自己写的Demo源码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值