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

相关内容

热门资讯

黑科技辅助!wepoke智能a... 黑科技辅助!wepoke智能ai,(AAPoKer德州局)竟然真的有挂,透明挂教程(有挂分享);黑科...
黑科技辅助!wepoke智能a... 您好,WePoKe辅助工具这款游戏可以开挂的,确实是有挂的,需要了解加微【485275054】很多玩...
黑科技辅助!wepoke黑科技... 黑科技辅助!wepoke黑科技ai设备,(WePoKe俱乐部)原本是有挂,黑科技教程(真的有挂),W...
黑科技辅助!wepoke辅助插... 黑科技辅助!wepoke辅助插件安装,(WePoKe能玩)固有真的是有挂,系统教程(有挂头条);We...
黑科技辅助!wepoke智能a... 黑科技辅助!wepoke智能ai,(AAPoKer德州)竟然是有挂,黑科技教程(有挂讲解)是一款可以...
黑科技辅助!wepoke辅助插... 【福星临门,好运相随】;黑科技辅助!wepoke辅助插件,(wpk线上德州)其实存在有挂,AA德州教...
黑科技辅助!wepoke透明黑... 黑科技辅助!wepoke透明黑科技,(红龙扑克功能)竟然真的是有挂,德州教程(有挂技术);1、让任何...
黑科技辅助!wepoke辅助插... 黑科技辅助!wepoke辅助插件安装,(智星德州菠萝闯关)好像存在有挂,分享教程(有挂教学);1、这...
黑科技辅助!wepoke透明黑... 黑科技辅助!wepoke透明黑科技,(AAPoKer操作)起初是有挂,教你攻略(有挂技巧);一、AA...
黑科技辅助!wepoke智能a... 黑科技辅助!wepoke智能ai辅助工具,(德扑之星机制)一向真的是有挂,玩家教程(有挂实锤)是一款...