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

相关内容

热门资讯

有挂技术开挂!多乐手游辅助软件... 有挂技术开挂!多乐手游辅助软件,越乡游辅助软件(发现玩家开挂辅助软件);亲,多乐手游辅助软件这款游戏...
有挂分析辅助!点我休闲辅助,w... 有挂分析辅助!点我休闲辅助,wepoker辅助器(透视有挂开挂辅助工具)【无需打开直接搜索加薇136...
有挂攻略开挂!老友广东潮汕麻雀... 有挂攻略开挂!老友广东潮汕麻雀,友空互娱插件(透视黑科技开挂辅助平台);打开点击测试直接进入微信(1...
有挂方法开挂!圣盛荆州晃晃辅助... 【亲,圣盛荆州晃晃辅助工具 这款游戏可以开挂的,确实是有挂的,很多玩家在这款圣盛荆州晃晃辅助工具中打...
有挂方法辅助!微乐自建房怎么提... >>您好:微乐自建房怎么提高胜率确实是有挂的,很多玩家在这款微乐自建房怎么提高胜率游戏中打牌都会发现...
揭秘有挂辅助!陕西三代辅助器免... 揭秘有挂辅助!陕西三代辅助器免费,小闲川南辅助工具(透视了解开挂辅助插件);无需打开直接搜索加(薇:...
有挂秘笈开挂!微乐家乡游戏攻略... 有挂秘笈开挂!微乐家乡游戏攻略,新芒果监利开机有挂吗(记者爆料开挂辅助工具) 了解更多开挂安装加(1...
有挂方法辅助!广东雀神挂机怎么... 有挂方法辅助!广东雀神挂机怎么样,新天道挂机辅助(教学盘点开挂辅助下载);无需打开直接搜索加薇136...
有挂头条辅助!奇迹陕西抢红包辅... 有挂头条辅助!奇迹陕西抢红包辅助器,越乡游义乌辅助器微信(科技分享开挂辅助神器);无需打开直接搜索薇...
有挂详细辅助!518互游辅助器... 有挂详细辅助!518互游辅助器免费下载,新海贝之城app破解(盘点十款开挂辅助插件);打开点击测试直...