Does someone know how to use PagerTitleStrip in Android

前端 未结 3 1319
我在风中等你
我在风中等你 2021-02-04 01:33

I decided to use a ViewPager in my application and everything is working fine. I Know that I want to use a PagerTitleStrip in my ViewPager

相关标签:
3条回答
  • 2021-02-04 02:12

    I'm not sure what causing error in your case but this is how I use it, in your layout you should insert following code in your layout:

    <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>
    

    Then you should add the following code in your PageAdapter:

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

    That's pretty it.

    0 讨论(0)
  • 2021-02-04 02:14

    I had this issue too where the PagerTitleStrip was not visible. The answers above did not help. The problem was that I followed the example on 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);
    

    The code above shows the view pager but not the PagerTitleStrip.

    I changed the code to

    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>
    

    I was also able to be backward compatible to API 8 by removing all the code in the ViewPager (javadoc page) example to manipulate the ActionBar and ActionBar.Tab.

    0 讨论(0)
  • 2021-02-04 02:18

    PagerTitleStrip is a non-interactive signal of the current, next, and former pages of a ViewPager. It really is designed to be used as a child view of a ViewPager widget

    Step 1. Create layout 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>
    

    Step 2. Create Activity contains ViewPager this (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);
     }
    }
    

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

    0 讨论(0)
提交回复
热议问题