在android.support.v4包中的两个控件PagerTabStrip与PagerTitleStrip,他们都是用来实现标题栏的,但各自有些不同,在这篇文章中,我们就讲讲它们各自都能实现怎样的功能,又有哪些异同点。
一、PagerTabStrip
PagerTabStrip的官方解释:
Class Overview
PagerTitleStrip is a non-interactive indicator of the current, next, and previous pages of a ViewPager
. It is intended to be used as a child view of a ViewPager widget in your XML layout. Add it as a child of a ViewPager in your layout file and set its android:layout_gravity to TOP or BOTTOM to pin it to the top or bottom of the ViewPager. The title from each page is supplied by the methodgetPageTitle(int)
in the adapter supplied to the ViewPager.
For an interactive indicator, see PagerTabStrip
.
总结起来 2点
1、在你的布局文件中,将它作为子控件添加在ViewPager中。
如:
<ViewPager>
<PagerTabStrip>
</PagerTabStrip>
</ViewPager>
2、标题的获取,是重写适配器的getPageTitle(int)方法来获取的。
一个例子
仅仅是 布局不同 一个有下划线 一个没有滑动的下划线
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<pre name="code" class="html">//<android.support.v4.view.Pager<span style="font-family:FangSong_GB2312;">Title</span>Strip<span style="font-family:FangSong_GB2312;">></span>
<android.support.v4.view.PagerTabStrip
android:id="@+id/pagerTabStrip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top" />
</android.support.v4.view.ViewPager>
</LinearLayout>
效果图
PagerTabStrip---<span style="color:#0660;"><span style="font-family:FangSong_GB2312;">滑动下划线</span></span>
PS:PagerTabStrip和PagerTitleStrip都不适合用在实际用途中