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。您可以根据需要调整指示器的起始和结束边距。

相关内容

热门资讯

透视苹果版!wepoker免费... 透视苹果版!wepoker免费透视脚本,wepoker辅助透视软件(详细辅助2025新版教程);1、...
WePoKe透视挂!wepok... WePoKe透视挂!wepoke人有挂吗(透视)竟然存在有挂(详细辅助微扑克教程)(1)WePoKe...
透视苹果版!wepoekr底牌... 透视苹果版!wepoekr底牌透视,wejoker内置辅助(详细辅助新2025教程)1、wepoek...
德州之星插件!wpk ai机器... 德州之星插件!wpk ai机器人和真的的区别(透视)本来是有挂(详细辅助切实教程)1、超多福利:超高...
we辅助poker德之星!德州... we辅助poker德之星!德州竞技联盟辅助(透视)固有真的是有挂(详细辅助解说技巧)是一款可以让一直...
透视免费!hhpoker免费透... 透视免费!hhpoker免费透视脚本,wepoker有透视吗(详细辅助微扑克教程)1)hhpoker...
WePoKe透视挂!poker... WePoKe透视挂!poker world外挂(透视)原先是真的有挂(详细辅助扑克教程)1、许多玩家...
透视科技!wpk透视脚本链接,... 透视科技!wpk透视脚本链接,德州透视是真的吗(详细辅助安装教程);1、玩家可以在wpk透视脚本链接...
we辅助poker德之星!GG... we辅助poker德之星!GG扑克有多假(透视)原来真的是有挂(详细辅助曝光教程)1、超多福利:超高...
透视ai代打!hhpoker开... 透视ai代打!hhpoker开辅助软件,wejoker内置辅助(详细辅助AI教程)1、每一步都需要思...