Android-浅谈自定义Tablayout布局

面对官方Tablayout API的不足,本文档介绍了如何自定义Tablayout以满足复杂需求。从背景、过程到总结,详细阐述了自定义布局的步骤,包括设置透明背景消除水波纹特效、实现横向滚动等关键点,旨在帮助开发者打造个性化布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景

查看官方文档,发现Tablayout的API太少,基本上满足不了现在市场各种奇葩的需求,所以就必须自定义布局了╮(╯▽╰)╭!

过程

废话不多说先上效果图:
最终效果图

  • 首先是准备工作
    1、MainActivity布局,就一组黄金搭档Tablayout+ViewPager,中间View只是一条分割线:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="0dp"
        android:layout_height="40dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:tabBackground="@android:color/transparent"
        app:tabIndicatorHeight="0dp"
        app:tabMode="scrollable" />

    <View
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tabLayout"></View>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tabLayout" />

</android.support.constraint.ConstraintLayout>

2、R.layout.item为自定义的Tablayout要显示的样式,这里很简单,只是一个TextView+ImageView(PS:布局根据需求灵活多变,想怎么玩儿就怎么玩儿):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:text="你好"
        android:textColor="@android:color/black"
        android:textSize="16sp"
        android:textStyle="bold" />

    <ImageView
        android:visibility="invisible"
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/tv"
        android:layout_alignLeft="@+id/tv"
        android:layout_alignRight="@+id/tv"
        android:layout_centerHorizontal="true"
        android:src="@mipmap/img_tablayout_line" />
</RelativeLayout>
  • 然后看具体的代码实现
public class MainActivity extends AppCompatActivity {
    private String[] title = {"one", "two", "three", "four", "five", "six", "seven"};
    private List<Fragment> fragmentList;
    private TabLayout tableLayout;

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

        tableLayout = findViewById(R.id.tabLayout);
        ViewPager viewPager = findViewById(R.id.viewPager);

        fragmentList = new ArrayList<>();
        for (int i = 0; i < title.length; i++) {
            fragmentList.add(new BlankFragment());
        }

        viewPager.setAdapter(new Adapter(getSupportFragmentManager()));
        tableLayout.setupWithViewPager(viewPager);

        getView();

        tableLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                setStatus(tab);
            }

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

            }

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

    /**
     * 获取Tablayout的自定义布局,并设置标题
     */
    private void getView() {
        for (int i = 0; i < tableLayout.getTabCount(); i++) {
            View view = LayoutInflater.from(this).inflate(R.layout.item, null);
            ((TextView) view.findViewById(R.id.tv)).setText(title[i]);
            tableLayout.getTabAt(i).setCustomView(view);
        }
        ((TextView) tableLayout.getTabAt(0).getCustomView().findViewById(R.id.tv)).setTextColor(Color.RED);
        tableLayout.getTabAt(0).getCustomView().findViewById(R.id.iv).setVisibility(View.VISIBLE);
    }

    /**
     * 设置tablayout选中状态
     *
     * @param tab
     */
    private void setStatus(TabLayout.Tab tab) {
        for (int i = 0; i < tableLayout.getTabCount(); i++) {
            if (i == tab.getPosition()) {
                ((TextView) tableLayout.getTabAt(i).getCustomView().findViewById(R.id.tv)).setTextColor(Color.RED);
                tableLayout.getTabAt(i).getCustomView().findViewById(R.id.iv).setVisibility(View.VISIBLE);
            } else {
                ((TextView) tableLayout.getTabAt(i).getCustomView().findViewById(R.id.tv)).setTextColor(Color.BLACK);
                tableLayout.getTabAt(i).getCustomView().findViewById(R.id.iv).setVisibility(View.INVISIBLE);
            }
        }
    }

    //为了省事儿Adapter写成内部类
    class Adapter extends FragmentPagerAdapter {

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

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

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

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

其中BlankFragment是创建的一个空白的Fragment,其实也没有什么技术难点,就是把自定义的布局setCustomView给Tablayout而已!

  • 最后也是一些细节地方需要注意的:
    1、在准备工作第1步中 app:tabBackground="@android:color/transparent"是为了Tablayout在点击的时候消除水波纹特效
    2、在准备工作第1步中 app:tabMode="scrollable"是为了Tablayout在标题过多的情况下可以横向滚动,其他属性可以查看官方API

总结

这里只是记录总结一下自定义的过程心得,希望可以帮到想要打造炫酷布局的你O(∩_∩)O哈哈~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值