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

相关内容

热门资讯

透视透视挂!pokemmo手机... 透视透视挂!pokemmo手机版脚本免费(透视)透明挂教程(2024已更新)(哔哩哔哩);1、首先打...
透视插件!aa poker辅助... 透视插件!aa poker辅助(透视)线上教程(2025已更新)(哔哩哔哩);1)aa poker辅...
透视美元局!aapoker插件... 透视美元局!aapoker插件(透视)插件(果然真的有挂);aapoker插件辅助器中分为三种模型:...
透视黑科技!哈糖大菠萝有没有挂... 透视黑科技!哈糖大菠萝有没有挂(透视)黑科技教程(2020已更新)(哔哩哔哩)哈糖大菠萝有没有挂软件...
透视免费!aapoker脚本怎... 透视免费!aapoker脚本怎么用(透视)透视软件(真是是有挂);1)aapoker脚本怎么用辅助挂...
透视肯定!pokemmo脚本手... 透视肯定!pokemmo脚本手机版(透视)揭秘教程(2026已更新)(哔哩哔哩);1、金币登录送、破...
透视科技!aapoker怎么控... 透视科技!aapoker怎么控制牌(透视)辅助(其实有挂);1、每一步都需要思考,不同水平的挑战会更...
透视教程!德州局透视脚本下载安... 透视教程!德州局透视脚本下载安装最新版本(透视)解密教程(2020已更新)(哔哩哔哩)1、不需要AI...
透视有挂!aapoker怎么拿... 透视有挂!aapoker怎么拿好牌(透视)透视脚本入口(原来存在有挂)1、不需要AI权限,帮助你快速...
透视实锤!德州透视脚本(透视)... 透视实锤!德州透视脚本(透视)我来教教你(2023已更新)(哔哩哔哩);1、打开软件启动之后找到中间...