有人知道如何在Android中使用PagerTitleStrip吗?

22
我决定在我的应用程序中使用ViewPager,一切都正常工作。 我知道我想在我的ViewPager中使用PagerTitleStrip,但我没有找到任何关于如何实现它的信息... 我发现了唯一一个关于这个类的页面,链接为http://developer.android.com/reference/android/support/v4/view/PagerTitleStrip.html。 所以看起来我只需要在我的ViewPager布局中添加PagerTitleStrip,但是我在我的活动中没有看到任何新的东西...
<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pagerTitleStrip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom" />
</android.support.v4.view.ViewPager>

有人知道如何使用它吗?

编辑: 抱歉,在ViewPagerAdapter中实现getPageTitle方法时可以工作...但我不想显示任何文本,只想显示一个小光标来显示当前视图与上一个和下一个视图的位置...

3个回答

46

我不确定什么原因导致了你的错误,但这是我使用的方法,在你的布局中,应该插入以下代码:

<android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" >

        <android.support.v4.view.PagerTitleStrip
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top" />
</android.support.v4.view.ViewPager>

然后您应该在PageAdapter中添加以下代码:

        @Override
        public CharSequence getPageTitle (int position) {
            return "Your static title";
        }

这就是它。


它的工作表现相当不错...它在定制方面并非最佳选择,但它能够胜任其工作。这应该是被接受的答案! - andrea.rinaldi

2

我也遇到了PagerTitleStrip不可见的问题。上面的答案没有帮助。问题在于我按照http://developer.android.com/reference/android/support/v4/view/ViewPager.html上的示例进行操作。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mViewPager = new ViewPager(this);
    mViewPager.setId(R.id.pager);
    setContentView(mViewPager);

以上代码显示了视图页面,但未显示PagerTitleStrip。

我将代码更改为

protected void onCreate(Bundle savedInstanceData) {
    super.onCreate(savedInstanceData);
    setTitle(R.string.my_title);

    setContentView(R.layout.my_pager);
    viewPager = (ViewPager)findViewById(R.id.pager);

res/layout/my_pager.xml:

 <?xml version="1.0" encoding="utf-8"?>
 <android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:textColor="#fff"
        android:paddingTop="4dp"
        android:paddingBottom="4dp" />

 </android.support.v4.view.ViewPager>

我还能通过删除ViewPager(javadoc页面)示例中用于操作ActionBar和ActionBar.Tab的所有代码,实现向后兼容API 8。


-1
PagerTitleStrip是ViewPager的当前、下一个和前一个页面的非交互式信号。它被设计为ViewPager小部件的子视图。
步骤1. 创建布局activity_main.xml。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.androidviewpagerapp.MainActivity" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />
    <android.support.v4.view.ViewPager
        android:id="@+id/myviewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v4.view.PagerTitleStrip
            android:id="@+id/titlestrip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </android.support.v4.view.ViewPager>
     </LinearLayout>

步骤2. 创建包含ViewPager的Activity(MainActivity.java)

public class MainActivity extends Activity {
 ViewPager viewPager;
 MyPagerAdapter myPagerAdapter;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  viewPager = (ViewPager)findViewById(R.id.myviewpager);
  myPagerAdapter = new MyPagerAdapter();
  viewPager.setAdapter(myPagerAdapter);
  PagerTitleStrip pagerTitleStrip = (PagerTitleStrip)findViewById(R.id.titlestrip);
 }
}

演示:http://photo-wonder.blogspot.com/2016/09/pagertitlestrip-on-viewpager-android.html

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接