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免费脚本咨询,pokemmo修改器手机版(透视)其实是真的挂(哔哩哔哩)1、...
透视方案!wpk作必弊最怕三个... 您好,如何判断wpk辅助软件的真假这款游戏可以开挂的,确实是有挂的,需要了解加去威信【1367043...
透视指引!pokerworld... 透视指引!pokerworld辅助器,wepoker透视脚本免费下载(透视)本来有挂(哔哩哔哩)we...
透视机巧!hhpoker透视脚... 透视机巧!hhpoker透视脚本,红龙poker作必弊指令(透视)其实存在有挂(哔哩哔哩)红龙pok...
透视绝活!拱趴大菠萝作必弊方法... 透视绝活!拱趴大菠萝作必弊方法,拱趴大菠萝十三水作必弊(透视)总是是真的透视技巧(哔哩哔哩)1、操作...
透视演示!wpk俱乐部是做什么... 透视演示!wpk俱乐部是做什么的,aapoker如何设置胜率(透视)切实真的有挂(哔哩哔哩)1、wp...
透视演示!aapoker脚本怎... 透视演示!aapoker脚本怎么用,aapoker辅助插件工具(透视)确实是真的脚本挂(哔哩哔哩)一...
透视大纲!pokemmo辅助脚... 透视大纲!pokemmo辅助脚本,aapoker辅助软件合法吗(透视)果然真的是有挂(哔哩哔哩)1、...
透视学习!aapoker万能辅... 透视学习!aapoker万能辅助器,aapoker辅助器是真的吗(透视)切实是有透视器(哔哩哔哩)亲...
透视操作!wepoker作必弊... 透视操作!wepoker作必弊视频,hhpoker是真的吗(透视)果然存在有挂(哔哩哔哩)进入游戏-...