Actionbar在viewpager中使用的标题效果

活动:

  1. /** 
  2.  * actionbar 
  3.  */  
  4. public class MainActivity extends FragmentActivity {  
  5.     private ViewPager mViewPager;  
  6.     private ActionBar mActionBar;  
  7.     private MyPagerAdapter mPagerAdapter;  
  8.     private List<String> titlelist = new ArrayList<String>();  
  9.     private List<Tab> tablist = new ArrayList<ActionBar.Tab>();  
  10.   
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_main);  
  15.         initView();  
  16.     }  
  17.   
  18.     public void initView() {  
  19.         titlelist.add("first");  
  20.         titlelist.add("second");  
  21.         titlelist.add("third");  
  22.         titlelist.add("fourth");  
  23.         titlelist.add("fifth");  
  24.   
  25.         mViewPager = (ViewPager) findViewById(R.id.vp_test);  
  26.         mPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(),  
  27.                 titlelist);  
  28.         mViewPager.setAdapter(mPagerAdapter);  
  29.         mViewPager.setOnPageChangeListener(mPageChangeListener);  
  30.         // actionbar  
  31.         mActionBar = getActionBar();  
  32.         mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);  
  33.         mActionBar.setDisplayShowTitleEnabled(false);  
  34.         mActionBar.setDisplayShowHomeEnabled(false);  
  35.   
  36.         for (int i = 0; i != titlelist.size(); i++) {  
  37.             tablist.add(mActionBar.newTab().setText(titlelist.get(i))  
  38.                     .setTabListener(mTabListener));  
  39.             mActionBar.addTab(tablist.get(i));  
  40.         }  
  41.   
  42.     }  
  43.   
  44.     private OnPageChangeListener mPageChangeListener = new OnPageChangeListener() {  
  45.   
  46.         @Override  
  47.         public void onPageSelected(int arg0) {  
  48.             mActionBar.setSelectedNavigationItem(arg0);  
  49.         }  
  50.   
  51.         @Override  
  52.         public void onPageScrolled(int arg0, float arg1, int arg2) {  
  53.   
  54.         }  
  55.   
  56.         @Override  
  57.         public void onPageScrollStateChanged(int arg0) {  
  58.   
  59.         }  
  60.     };  
  61.   
  62.     private TabListener mTabListener = new TabListener() {  
  63.   
  64.         @Override  
  65.         public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {  
  66.             if (tab == tablist.get(0)) {  
  67.                 mViewPager.setCurrentItem(0);  
  68.             } else if (tab == tablist.get(1)) {  
  69.                 mViewPager.setCurrentItem(1);  
  70.             } else if (tab == tablist.get(2)) {  
  71.                 mViewPager.setCurrentItem(2);  
  72.             }  
  73.   
  74.         }  
  75.   
  76.         @Override  
  77.         public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {  
  78.             // TODO Auto-generated method stub  
  79.   
  80.         }  
  81.   
  82.         @Override  
  83.         public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {  
  84.             // TODO Auto-generated method stub  
  85.   
  86.         }  
  87.     };  
  88. }  

 

适配器:

  1. public class MyPagerAdapter extends FragmentPagerAdapter {  
  2.   
  3.     private List<String> titlelist;  
  4.   
  5.     public MyPagerAdapter(FragmentManager fm, List<String> titlelist) {  
  6.         super(fm);  
  7.         // TODO Auto-generated constructor stub  
  8.         this.titlelist = titlelist;  
  9.     }  
  10.   
  11.     @Override  
  12.     public Fragment getItem(int position) {  
  13.         // TODO Auto-generated method stub  
  14.         return MyFragment.create(titlelist.get(position));  
  15.     }  
  16.   
  17.     @Override  
  18.     public int getCount() {  
  19.         // TODO Auto-generated method stub  
  20.         return titlelist.size();  
  21.     }  
  22.   
  23.     public static class MyFragment extends Fragment {  
  24.         public static MyFragment create(String address) {  
  25.             MyFragment f = new MyFragment();  
  26.             Bundle b = new Bundle();  
  27.             b.putString("address", address);  
  28.             f.setArguments(b);  
  29.             return f;  
  30.         }  
  31.   
  32.         @Override  
  33.         public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  34.                 Bundle savedInstanceState) {  
  35.             Random r = new Random(System.currentTimeMillis());  
  36.             Bundle b = getArguments();  
  37.             View v = inflater.inflate(R.layout.fram, null);  
  38.             v.setBackgroundColor(r.nextInt() >> 8 | 0xFF <24);  
  39.             return v;  
  40.         }  
  41.   
  42.     }  
  43. }  

main xml:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <android.support.v4.view.ViewPager  
  7.         android:id="@+id/vp_test"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content" />  
  10.   
  11. </RelativeLayout>  


fragement xml:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/textView1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="this is a fram"  
  12.         android:textSize="30sp" />  
  13.   
  14. </LinearLayout>  


效果:



注意:

1.改变actionbar的背景颜色:

  1. <style name="MyActionBar" parent="@android:style/Widget.Holo.ActionBar">  
  2.         <item name="android:windowActionBarOverlay">true</item>  
  3.         <item name="android:height">50dp</item>  
  4.         <item name="android:background">#695cca</item>  
  5.         <!-- title背景 -->  
  6.         <item name="android:backgroundStacked">#06031f</item>  
  7.         <item name="android:backgroundSplit">#dc8508</item>  
  8.     </style>  
  9.   
  10.     <style name="MyActionBarTabBarStyle" parent="@android:style/Widget.Holo.ActionBar.TabBar">  
  11.         <item name="android:divider">?android:attr/actionBarDivider</item>  
  12.         <item name="android:showDividers">none</item>  
  13.     </style>  
  14.   
  15.     <style name="MyActionBarTabStyle" parent="@android:style/Widget.Holo.ActionBar.TabView">  
  16.         <item name="android:paddingTop">10dp</item>  
  17.         <!-- 整个tab的背景 -->>  
  18.         <!--  <item name="android:background">#680749</item>-->  
  19.     </style>  
  20.   
  21.     <style name="Widget.Holo.ActionBar.TabText" parent="@android:style/Widget.ActionBar.TabText">  
  22.         <!-- 字体颜色 -->>  
  23.         <item name="android:textColor">@android:color/holo_green_dark</item>  
  24.         <item name="android:textSize">15sp</item>  
  25.         <item name="android:textStyle">bold</item>  
  26.         <item name="android:textAllCaps">true</item>  
  27.         <item name="android:ellipsize">marquee</item>  
  28.         <item name="android:maxLines">1</item>  
  29.     </style>  


在主题中引用:

    1. <style name="AppTheme" parent="@android:style/Theme.Holo.Light">  
    2.        <item name="android:windowAnimationStyle">@style/AnimationAc</item>  
    3.        <item name="android:windowContentOverlay">@null</item>  
    4.        <item name="android:actionBarStyle">@style/MyActionBar</item>  
    5.        <item name="android:actionBarTabBarStyle">@style/MyActionBarTabBarStyle</item>  
    6.        <item name="android:actionBarTabStyle">@style/MyActionBarTabStyle</item>  
    7.        <item name="android:actionBarTabTextStyle">@style/Widget.Holo.ActionBar.TabText</item>  
    8.    </style> 
posted @ 2016-11-05 16:18  天涯海角路  阅读(145)  评论(0)    收藏  举报