PagerTabStrip簡單使用方式1


效果圖示例:java

 

 

 

 

 

 

//res/layout下5個佈局一、activity_main.xml 二、fragment_view1.xml 三、fragment_view2.xml
//四、fragment_view3.xml 五、fragment_view4.xml
android

 

 

一、activity_main.xml佈局ide


代碼佈局


<RelativeLayout 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"
    tools:context="${relativePackage}.${activityClass}" >
<!-- ViewPager至關於一個容器包裹PagerTabStrip -->
    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >字體

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

</RelativeLayout>spa

 

 


================xml

 

 

二、fragment_view1.xml佈局ip


代碼ci

<RelativeLayout 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"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="這是記錄頁面"/>

</RelativeLayout>

 

=================


三、fragment_view2佈局

代碼


<RelativeLayout 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"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:id="@+id/textview2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="這是聯繫人頁面"/>

</RelativeLayout>

 

=====================


四、fragment_view3佈局

代碼

<RelativeLayout 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"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:id="@+id/textview3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="這是收藏夾頁面"/>

</RelativeLayout>

 

===================


五、fragment_view4佈局

代碼


<RelativeLayout 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"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:id="@+id/textview4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="這是羣組頁面"/>

</RelativeLayout>

 

 

 

=====================


六、MainActivity.java類


代碼

 

public class MainActivity extends Activity {

 private ViewPager viewPager;
 private PagerTabStrip pagerTabStrip;
 
 private List<View> list_view;
 private List<String> list_title;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  this.viewPager = (ViewPager) this.findViewById(R.id.viewPager);
  this.pagerTabStrip = (PagerTabStrip) this.findViewById(R.id.pagerTabStrip);
  
  //設置pagerTabStrip的一些須要屬性
  pagerTabStrip.setTextColor(Color.WHITE);//字體顏色白色
  pagerTabStrip.setBackgroundColor(Color.BLACK);//背景黑色
  pagerTabStrip.setDrawFullUnderline(true);//設置下劃線
  pagerTabStrip.setTabIndicatorColor(Color.CYAN);//選中的下劃線顏色
  pagerTabStrip.setTextSpacing(40);//每一個tab的間隔
  
  list_view = new ArrayList<View>();
  LayoutInflater inflater = LayoutInflater.from(this);
  list_view.add(inflater.inflate(R.layout.fragment_view1, null));
  list_view.add(inflater.inflate(R.layout.fragment_view2, null));
  list_view.add(inflater.inflate(R.layout.fragment_view3, null));
  list_view.add(inflater.inflate(R.layout.fragment_view4, null));
  
  list_title = new ArrayList<String>();
  list_title.add("記錄");
  list_title.add("聯繫人");
  list_title.add("收藏夾");
  list_title.add("羣組");
  
  viewPager.setAdapter(new MyPagerAdapter(list_view));
  
 }
 class MyPagerAdapter extends PagerAdapter{
  
  private List<View> list_view;
  public MyPagerAdapter(List<View> list_view) {
   this.list_view = list_view;
  }

  @Override  public int getCount() {   return this.list_view.size();  }    @Override  public boolean isViewFromObject(View arg0, Object arg1) {   return arg0 == arg1;  }    @Override  public Object instantiateItem(ViewGroup container, int position) {   container.addView(this.list_view.get(position));   return this.list_view.get(position);  }  @Override  public void destroyItem(ViewGroup container, int position, Object object) {   container.removeView(this.list_view.get(position));    //把pagerTabStrip和ViewPager關聯起來  @Override  public CharSequence getPageTitle(int position) {   return list_title.get(position);  } }}

相關文章
相關標籤/搜索