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怎么买辅助,wepoker手机插件-总是存在有辅助教程(哔哩哔哩)1、不需要...
透视曝光!wepoker提高好... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...
透视总结!wepoker破解是... 透视总结!wepoker破解是真的还是假的,wepoker怎么增加运气-真是存在有辅助教程(哔哩哔哩...
透视揭幕!wepoker透视脚... 透视揭幕!wepoker透视脚本,wepoker免费脚本咨询-都是真的有辅助神器(哔哩哔哩)1、游戏...
透视专业!wepoker透视脚... 透视专业!wepoker透视脚本免费app,wepoker怎么挂底牌-一直一直总是有辅助插件(哔哩哔...
透视推荐!wpk透视辅助,wp... 透视推荐!wpk透视辅助,wpk安卓下载辅助-一贯有辅助插件(哔哩哔哩)1、用户打开应用后不用登录就...
透视有挂!购买的wpk辅助在哪... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩家在这款游戏中打...
透视教你!wpk透视辅助方法,... 透视教你!wpk透视辅助方法,wpk插件-一贯是真的有辅助软件(哔哩哔哩)1、不需要AI权限,帮助你...
透视了解!aapoker怎么选... 透视了解!aapoker怎么选牌,aapoker透视脚本-原来一直都是有辅助app(哔哩哔哩)1、玩...
透视曝光!hhpoker辅助挂... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩家在这款游戏中打...