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、点击下载安装,...
外挂攻略!poker worl... 外挂攻略!poker world辅助,德州透视脚本-原来是有辅助工具(哔哩哔哩)1、许多玩家不知道德...
外挂指引!佛手在线有挂吗,菠萝... 外挂指引!佛手在线有挂吗,菠萝德州透视脚本-切实有辅助工具(哔哩哔哩)暗藏猫腻,小编详细说明菠萝德州...
外挂策略!菠萝辅助器免费版的特... 外挂策略!菠萝辅助器免费版的特点,poker world辅助-其实真的有辅助技巧(哔哩哔哩)该软件可...
外挂讲义!德州透视是真的吗,约... 外挂讲义!德州透视是真的吗,约局吧德州有挂吗-总是是真的有辅助app(哔哩哔哩)亲,关键说明,德州透...
外挂诀窍!werplan外开挂... 外挂诀窍!werplan外开挂,pokernow辅助控制-本来是有辅助工具(哔哩哔哩);1、上手简单...
外挂演示!werplan怎么作... 外挂演示!werplan怎么作必弊,德州机器人代打脚本-总是一直都是有辅助插件(哔哩哔哩)1、在德州...
外挂经验!约局吧作必弊脚本,来... 外挂经验!约局吧作必弊脚本,来玩app破解-一贯存在有辅助app(哔哩哔哩)约局吧作必弊脚本破解侠是...
外挂妙计!佛手大菠萝辅助,we... 外挂妙计!佛手大菠萝辅助,werplan怎么作必弊-好像真的是有辅助技巧(哔哩哔哩)1、超多福利:超...
外挂练习!拱趴游戏破解器,ne... 外挂练习!拱趴游戏破解器,newpoker脚本-一贯一直都是有辅助教程(哔哩哔哩)亲,关键说明,拱趴...