Android Tablayout - 简书

前言

Tablayout继承自HorizontalScrollView,用作页面切换指示器,因使用简便功能强大而广泛使用在App中。

但有的产品经理偏偏是个磨人的小妖精,真的猜不透啊,今天要这种效果,明天就指着另一款App说做成跟这个一样。对付这种产品经理我们有骨气的程序员该怎么办?怎么办?当然是屈服啊,不然去跳楼让他因内疚改需求?

所以,就在各种复杂的情况下打磨,渐渐地就变得圆润。这里,有Tablayout的各种使用场景,拿去指着产品经理说:你要哪个给你哪个。

常用属性:

app:tabIndicatorColor :指示线的颜色

app:tabIndicatorHeight :指示线的高度

app:tabSelectedTextColor : tab选中时的字体颜色

app:tabMode="scrollable" : 默认是fixed,固定的;scrollable:可滚动的

链接:https://www.jianshu.com/p/fde38f367019

2.设置Tablayout属性:

用Tablayout属性写一个style,给需要的Tablayout引用。

    <style name="MyTablayoutstyle" parent="Base.Widget.Design.TabLayout">

        <item name="tabBackground">@color/white</item>

        <item name="tabIndicatorColor">@color/green</item>

        <item name="tabIndicatorHeight">2dp</item>

        <item name="tabSelectedTextColor">@color/green</item>

        <item name="android:textSize">15sp</item>

        <item name="android:textColor">@color/text</item>

    </style>

Tablayout引用:

    <android.support.design.widget.TabLayout

        android:id="@+id/tab1"

        style="@style/MyTablayoutstyle"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"/>

实现Tablayout代码:

private void initDate() {

    list = new ArrayList<>();

    list.add(new Fragment4());

    list.add(new Fragment5());

    viewPager.setAdapter(new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {

        @Override

        public Fragment getItem(int i) {

            return list.get(i);

        }

        @Override

        public int getCount() {

            return list.size();

        }

    });

    tab.addTab(tab.newTab());

    tab.addTab(tab.newTab());

    tab.setupWithViewPager(viewPager);

    tab.getTabAt(0).setText(R.string.app_name1);

    tab.getTabAt(1).setText(R.string.app_name2);

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容