效果图如下:
虽然很简单,但是还是将主要代码贴出来,以免以后忘记
mTabWidth = (int) (Global.mScreenWidth / mTabs.length);
mTabIndicator = findViewById(R.id.tab_indicator);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)
mTabIndicator.getLayoutParams();
params.width = mTabWidth; // 设置指示线的宽度
/** 滚动选项卡指示线 */
private void scrollTabIndicator(int position, float percent) {
// 左边距
int marginLeft = (int) (mTabWidth * position + mTabWidth * percent);
// 方式一:
// mTabIndicator.setTranslationX(marginLeft);
// 方式二:
LinearLayout.LayoutParams params =
(LinearLayout.LayoutParams) mTabIndicator.getLayoutParams();
params.leftMargin = marginLeft;
mTabIndicator.setLayoutParams(params);
}
在最先开始运行的类中记得要先初始化,否则指示线的效果出不来
Global.init(this);