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...
WPK下载透视手册!wepok... WPK下载透视手册!wepoker破解器有用吗,wpkai多功能透视插件1、下载好wepoker破解...
实操分享辅助挂!非常竞技ai辅... 您好,实操分享辅助挂!非常竞技ai辅助挂多功能插件,最初有辅助挂平台办法这款游戏可以开挂的,确实是有...
WPK官网辅助练习!德普之星透... WPK官网辅助练习!德普之星透视辅助软件激活码,AApokerai多功能辅助工具一、德普之星透视辅助...
一分钟了解辅助挂!约战荆门麻将... 约战荆门麻将AI 游戏攻略黑科技上线本地缓存功能,离线查阅能力开挂。提前下载对应游戏攻略数据包,断网...
WPK真假透视挂操作!有哪些免... WPK真假透视挂操作!有哪些免费的wpk作弊码,红龙PoKerai多功能透视挂工具1、有哪些免费的w...
细节方法辅助!聚游广东麻将ai... 很多人使用云端 AI 时,都担心聚游广东麻将、内部资料上传带来隐私风险。这款本地运行的 AI 黑科技...
WPK俱乐部辅助指引!wpk作... WPK俱乐部辅助指引!wpk作弊最怕三个东西,德扑之星ai多功能辅助app1、很好的工具软件,可以解...
查到实测辅助器!科乐四平麻将a... 您好:科乐四平麻将这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别...
WPK透视透视挂绝活儿!wep... 您好,wepoker软件辅助程序这款游戏可以开挂的,确实是有挂的,需要了解加去威信【13670430...