如何显示ViewPager标题?

时间:2014-03-18 12:34:10

标签: android android-viewpager

您好我必须在我的应用中实施ViewPager。在这里我必须加载ImageAdapter扩展PagerAdapter,我成功地将所有图像都看到了viewpager。

现在,我想将viewpager标题显示为我的图像计数器。

Layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/gallery_detail_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/black" >

<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/rel_ig" >
</android.support.v4.view.ViewPager>
</RelativeLayout>

请有人告诉我我想怎么做!

2 个答案:

答案 0 :(得分:2)

首先需要将android.support.v4.view.PagerTabStrip添加到Layout.xml文件中,如:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/gallery_detail_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/black" >

<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/rel_ig" >

    <android.support.v4.view.PagerTabStrip
        android:id="@+id/pts_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</android.support.v4.view.ViewPager>
</RelativeLayout>

现在,覆盖CharSequence getPageTitle(int position)方法int Adapter,如:

    public class ImageAdapter extends PagerAdapter {

     ............ 
     @Override
     public CharSequence getPageTitle(int position) {
         return (position + 1);
         }
    ...........
}

并在您的PagerTabStrip中实施:您可以自定义PagerTabStrip选项

PagerTabStrip strip = (PagerTabStrip)view.findViewById(R.id.pts_main);
strip.setDrawFullUnderline(false);
strip.setTabIndicatorColor(Color.DKGRAY);
strip.setBackgroundColor(Color.GRAY);
strip.setNonPrimaryAlpha(0.5f);
strip.setTextSpacing(25);
strip.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);

答案 1 :(得分:1)

试试这个: -

activity_main.xml 中添加 PagerTitleStrip

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

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

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

</LinearLayout>

然后在 PageAdapter.java 中添加以下方法:

@Override
    public CharSequence getPageTitle(int position) {
        return "Title Here";
    }