2011-12-21 20 views
22

我決定在我的應用程序中使用ViewPager,並且一切正常。 我知道我想在我的ViewPager中使用PagerTitleStrip,但我沒有找到任何有關如何操作的信息... 我在這堂課上找到的唯一一頁(原文如此!)是http://developer.android.com/reference/android/support/v4/view/PagerTitleStrip.html 因此,似乎我只需要我的ViewPager佈局中添加PagerTitleStrip,但我沒有看到我的活動什麼新的東西......有人知道如何在Android中使用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> 

有誰知道如何使用它?

編輯: 對不起它落實在ViewPagerAdaptergetPageTitle方法時工作......但我不希望任何文本顯示,只是一個小光標顯示相比於前面和後面的那些當前視圖的位置...

回答

44

我不知道你的情況造成的錯誤,但我這是怎麼用它,在你的佈局,你應該插入按照你的佈局代碼:

<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"; 
     } 

這就好了。

+0

它工作得很好......這不是最好的定製方面,但它的工作。這應該是被接受的答案! – 2015-09-10 08:21:35

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 /佈局/ 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除去所有的代碼是向後兼容API 8 (javadoc頁)示例來操作ActionBar和ActionBar.Tab。

-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這個(MainActvity。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

相關問題