/** * 这个方法可以为某一个值 * 赋值 */ DisplayMetrics dm = getResources().getDisplayMetrics();
scrollOffset = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, scrollOffset, dm); indicatorHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, indicatorHeight, dm); underlineHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, underlineHeight, dm); dividerPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dividerPadding, dm); tabPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, tabPadding, dm); dividerWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dividerWidth, dm); tabTextSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, tabTextSize, dm);
/** * 取出xml中配置的属性 */ a = context.obtainStyledAttributes(attrs, R.styleable.PagerSlidingTabStrip);
indicatorColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsIndicatorColor, indicatorColor); underlineColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsUnderlineColor, underlineColor); dividerColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsDividerColor, dividerColor); indicatorHeight = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsIndicatorHeight, indicatorHeight); underlineHeight = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsUnderlineHeight, underlineHeight); dividerPadding = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsDividerPadding, dividerPadding); tabPadding = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsTabPaddingLeftRight, tabPadding); tabBackgroundResId = a.getResourceId(R.styleable.PagerSlidingTabStrip_pstsTabBackground, tabBackgroundResId); shouldExpand = a.getBoolean(R.styleable.PagerSlidingTabStrip_pstsShouldExpand, shouldExpand); scrollOffset = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsScrollOffset, scrollOffset); textAllCaps = a.getBoolean(R.styleable.PagerSlidingTabStrip_pstsTextAllCaps, textAllCaps);
//初始化画笔,用于,一个矩形 rectPaint = new Paint(); rectPaint.setAntiAlias(true); rectPaint.setStyle(Style.FILL);
/** * 绑定一个listener 版定之后,外部不能重复版定,重复版定,这个就会失效了 */ pager.setOnPageChangeListener(pageListener);
/** * 得到视图树的监听者,,得到一个空间布局完成 */ getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @SuppressWarnings("deprecation") @SuppressLint("NewApi") @Override public void onGlobalLayout() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { getViewTreeObserver().removeGlobalOnLayoutListener(this); } else { getViewTreeObserver().removeOnGlobalLayoutListener(this); } currentPosition = pager.getCurrentItem(); scrollToChild(currentPosition, 0); } });
/** * 修改样式_只是默认时候的字体颜色以及字体大小 * 修改样式,,修改的只是默认时候的字体颜色和默认大小 */ private void updateTabStyles() { for (int i = 0; i < tabCount; i++) { View v = tabsContainer.getChildAt(i); v.setBackgroundResource(tabBackgroundResId); if (v instanceof TextView) { TextView tab = (TextView) v; tab.setTextSize(TypedValue.COMPLEX_UNIT_PX, tabTextSize); tab.setTypeface(tabTypeface, tabTypefaceStyle); tab.setTextColor(tabTextColor); // setAllCaps() is only available from API 14, so the upper case is made manually if we are on a // pre-ICS-build if (textAllCaps) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { tab.setAllCaps(true); } else { tab.setText(tab.getText().toString().toUpperCase(locale)); } } } } }
/** * 绘制 矩形 */ canvas.drawRect(lineLeft, height - indicatorHeight, lineRight, height, rectPaint);
下面是参考别人的分析
用法
对于这个项目的执行工作看到了
sample/
文件夹。
- 包括在以下依赖
build.gradle
文件。compile 'com.jpardogo.materialtabstrip:library:1.1.1'
或库中添加作为项目。我试图发送一个拉入请求,但看起来像原开发商不能维持下去了。
- 包括
PagerSlidingTabStrip
在布局小部件。这通常应放在上面的ViewPager
它代表。<com.astuetz.PagerSlidingTabStrip android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" />
- 在
onCreate
方法(或onCreateView
为一个片段),结合小窗口的ViewPager
:// Initialize the ViewPager and set an adapter ViewPager pager = (ViewPager) findViewById(R.id.pager); pager.setAdapter(new TestAdapter(getSupportFragmentManager())); // Bind the tabs to the ViewPager PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs); tabs.setViewPager(pager);
这就是所有你需要做的,但如果你想使用自己的选项卡,然后...
如果您的适配器实现接口
CustomTabProvider
可以粘贴您的自定义选项卡视图/秒。
在情况下,视图返回包含ID
R.id.psts_tab_title
,该视图应该是一个TextView
和将被用于放置在标题和设置视图状态(按压/选择/默认值)。如果你不希望库管理您的TextView标题标签做的,使用不同的ID不是
R.id.psts_tab_title
你的标签布局。该接口提供了标签的选择和去除选择回调也是如此。
如果您的适配器没有实现接口
CustomTabProvider
的默认选项卡将被使用,这是一个TextView
带有IDR.id.psts_tab_title
)。(可选)如果您使用
OnPageChangeListener
你的看法寻呼机,你应该将其设置在小部件,而不是直接在寻呼机。// continued from above tabs.setOnPageChangeListener(mPageChangeListener);
定制
从主题:
android:textColorPrimary
值(从你的主题)将如果值不上的XML布局定义被自动应用到该标签的文本颜色,underlineColor,dividerColor和indicatorColor(与150阿尔法255α和非选定标签选择的选项卡),。
注意事项的一些原生属性:
android:paddingLeft
或android:paddingRight
布局填充。如果同时应用,他们应该是平衡的。检查问题#69获取更多信息。
自定义属性:
pstsIndicatorColor
滑动指示剂变色。textColorPrimary
将它的默认颜色值。pstsIndicatorHeight
滑动指示器的高度。pstsUnderlineColor
上的视图的底部的全宽线的颜色。textColorPrimary
将它的默认颜色值。pstsUnderlineHeight
上的视图的底部的全宽线的高度。pstsDividerColor
标签之间的分界线的颜色。textColorPrimary
将它的默认颜色值。pstsDividerWidth
分隔线的笔划宽度,缺省值为0。pstsDividerPadding
分频器的顶部和底部填充。pstsShouldExpand
如果设置为true,每个选项卡被赋予相同的权重,默认为false。pstsScrollOffset
滚动偏移所选标签的。pstsPaddingMiddle
如果为真,的选项卡视图(像报摊谷歌应用程序)的中间开始。pstsTabPaddingLeftRight
每个选项卡的左右填充。pstsTabBackground
每个选项卡的背景绘制的,应该是一个StateListDrawable。pstsTabTextSize
标签文字大小(SP)。pstsTabTextColor
标签文本颜色,它们可以是一种颜色(文本颜色不会改变)或每个状态的颜色的选择:按压(标签按下),选择(选项卡激活),默认(活动非活动)。国家在选择的顺序很重要。检查问题#68获取更多信息。pstsTabTextStyle
设置文本样式,默认为正常的API 21,大胆的在旧的API。pstsTabTextAllCaps
如果为真,所有的选项卡标题将是大写,默认值是true。pstsTabTextAlpha
设置非选定的标签文本的Alpha透明度。范围0..255。150是它的默认值。这将不如果使用pstsTabTextColor
在布局定义。如果pstsTabTextColor
是NOT定义,将施加到非选择的选项卡。pstsTabTextFontFamily
设置字体系列名称。默认sans-serif-medium
的API 21,sans-serif
在旧的API。
几乎所有的属性都有其各自的getter和setter方法在运行时改变它们。动态地改变pstsTabTextFontFamily
和pstsTabTextStyle
您可以拨打:
public void setTypeface(Typeface typeface, int style)
。它可用于在默认选项卡来定义自定义字体。否则,你可以使用自定义选项卡用CustomTabProvider
。