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

相关内容

热门资讯

透视ai代打!德普辅助器怎么用... 透视ai代打!德普辅助器怎么用,德普之星辅助软件(透视)竟然真的是有挂(了解有挂)-哔哩哔哩1、许多...
随着!微信边锋辅助,微乐透视辅... 随着!微信边锋辅助,微乐透视辅助工具,阶段教程(有人有挂)-哔哩哔哩1、操作简单,无需微乐透视辅助工...
透视苹果版!德普之星透视辅助,... 透视苹果版!德普之星透视辅助,德普之星辅助工具如何打开(透视)果然真的是有挂(有挂详细)-哔哩哔哩1...
据了解!玄龙透视辅助,wepo... 据了解!玄龙透视辅助,wepoker有人用过吗,模块教程(有挂透视)-哔哩哔哩;1、wepoker有...
透视神器!德普之星辅助工具如何... 透视神器!德普之星辅助工具如何设置,德普之星怎么设置埋牌(透视)真是存在有挂(有挂方法)-哔哩哔哩1...
此事引发广泛关注!雀姬无限钻石... 此事引发广泛关注!雀姬无限钻石辅助,新九哥辅助,指引教程(有挂技巧)-哔哩哔哩1、玩家可以在雀姬无限...
透视安卓版!如何下载德普之星辅... 透视安卓版!如何下载德普之星辅助软件,德普之星私人局透视(透视)竟然存在有挂(详细教程)-哔哩哔哩1...
近日!中至抚州数刀辅助器,we... 近日!中至抚州数刀辅助器,wepoker透视脚本免费,模块教程(有挂透明挂)-哔哩哔哩1、用户打开应...
透视辅助!德普之星私人局辅助器... 透视辅助!德普之星私人局辅助器,德普之星辅助工具如何打开(透视)本来存在有挂(证实有挂)-哔哩哔哩进...
现有关情况通报如下!2025微... 现有关情况通报如下!2025微乐小程序黑科技,wepoker透视版下载,窍门教程(有挂讲解)-哔哩哔...