Android ViewPager和TabLayout:如何在边距之间有线性(矩形)指示器
创始人
2024-08-19 05:00:16
0

要在Android ViewPager和TabLayout中实现具有线性(矩形)指示器的边距之间,您可以使用自定义的TabLayout.TabIndicatorColorStateList。以下是一个示例代码:

  1. 创建一个名为CustomTabLayout的类,继承自TabLayout。
public class CustomTabLayout extends TabLayout {

    private int indicatorStartMargin;
    private int indicatorEndMargin;

    public CustomTabLayout(Context context) {
        super(context);
    }

    public CustomTabLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setIndicatorMargins(int startMargin, int endMargin) {
        this.indicatorStartMargin = startMargin;
        this.indicatorEndMargin = endMargin;
        invalidate();
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

        View currentTabView = getTabAt(getSelectedTabPosition()).view;
        int tabWidth = currentTabView.getWidth();

        int indicatorStart = currentTabView.getLeft() + indicatorStartMargin;
        int indicatorEnd = currentTabView.getRight() - indicatorEndMargin;

        int indicatorTop = getHeight() - getSelectedTabIndicator().getIntrinsicHeight();
        int indicatorBottom = getHeight();

        getSelectedTabIndicator().setBounds(indicatorStart, indicatorTop, indicatorEnd, indicatorBottom);
        getSelectedTabIndicator().draw(canvas);
    }
}
  1. 在您的布局文件中,将TabLayout替换为CustomTabLayout

  1. 在您的活动或碎片中,通过调用setIndicatorMargins()来设置指示器的边距。
CustomTabLayout tabLayout = findViewById(R.id.tabLayout);
tabLayout.setIndicatorMargins(16, 16);

这样,您就可以在边距之间实现具有线性(矩形)指示器的Android ViewPager和TabLayout。您可以根据需要调整指示器的起始和结束边距。

相关内容

热门资讯

玩家爆料"微乐小程序... 玩家爆料"微乐小程序黑科技"开挂(插件)辅助插件有挂细节-2026教程 了解更多开挂安装加(1367...
解密关于"陕西微乐小... 解密关于"陕西微乐小程序辅助"开挂(安装)辅助安装有挂规律-德州教程>>您好:软件加13670430...
透视黑科技"白银胡乐... 透视黑科技"白银胡乐辅助脚本下载"开挂(平台)辅助平台有人有挂-教你攻略 >>您好:软件加薇1367...
透视辅助"川娱竞技辅... 透视辅助"川娱竞技辅助"开挂(平台)辅助平台新版有挂-科技教程《详细加薇136704302咨询》游戏...
指导大家"菠萝德普辅... 指导大家"菠萝德普辅助器免费版在哪里"开挂(脚本)辅助脚本有挂方针-爆料教程这是一款可以让一直输的玩...
一分钟了解"越乡游金... 较多好评“微乐万能挂官网”开挂(透视)辅助教程 了解更多开挂安装加(136704302)微信号是一款...
查到实测"顺欣茶坊辅... 查到实测"顺欣茶坊辅助器下载"开挂(插件)辅助插件有挂头条-曝光教程【无需打开直接搜索加薇13670...
盘点几款"蜂娱辅助&... 盘点几款"蜂娱辅助"开挂(软件)辅助软件有挂教学-大神讲解您好:蜂娱辅助这款游戏可以开挂,确实是有挂...
技巧知识分享"花花生... 花花生活圈开挂是一款专注玩家量身打造的游戏记牌类型软件,在花花生活圈开挂这款游戏中我们可以记录下每张...
总算了解"wepok... 总算了解"wepoker俱乐部辅助器"开挂(安装)辅助安装有挂透明挂-必胜教程>>您好:软件加136...