码迷,mamicode.com
首页 > 其他好文 > 详细

简单的导航viewpager

时间:2015-04-16 19:39:15      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

下载PagerSlidingTabStrip        https://github.com/astuetz/PagerSlidingTabStrip

 

主界面:

 

 1 public class MainActivity extends FragmentActivity {
 2 
 3     private ViewPager viewPager;
 4     private PagerSlidingTabStrip tab;
 5     private String[] Titles={"页面一","页面二","页面三","页面四"};
 6     private DisplayMetrics dm;
 7     @Override
 8     protected void onCreate(Bundle savedInstanceState) {
 9         super.onCreate(savedInstanceState);
10         setContentView(R.layout.activity_main);
11         viewPager = (ViewPager) findViewById(R.id.viewPager);
12         viewPager.setAdapter(new TestAdapter(getSupportFragmentManager()));
13         tab=(PagerSlidingTabStrip) findViewById(R.id.tabStrip);
14         tab.setViewPager(viewPager);
15         
16         dm = getResources().getDisplayMetrics();
17         // 设置填充满屏幕
18         tab.setShouldExpand(true);
19         // 设置分割线
20         tab.setDividerColor(Color.TRANSPARENT);
21         // 设置底部线的高度
22         tab.setUnderlineHeight((int) TypedValue.applyDimension(
23                 TypedValue.COMPLEX_UNIT_DIP, 1, dm));
24         // 设置Indicator的高度
25         tab.setIndicatorHeight((int) TypedValue.applyDimension(
26                 TypedValue.COMPLEX_UNIT_DIP, 5, dm));
27         // 标题文字的大小
28         tab.setTextSize((int) TypedValue.applyDimension(
29                 TypedValue.COMPLEX_UNIT_SP, 18, dm));
30         // Indicator的颜色
31         tab.setIndicatorColor(Color.parseColor("#569844"));
32         // 设置选中文字的颜色 
33         tab.setSelectedTextColor(Color.parseColor("#569844"));
34         // 点击的背景色
35         tab.setTabBackground(0);
36     }
37     
38     class TestAdapter extends FragmentPagerAdapter{
39 
40         public TestAdapter(FragmentManager fm) {
41             super(fm);
42             // TODO Auto-generated constructor stub
43         }
44 
45         @Override
46         public Fragment getItem(int position) {
47             // TODO Auto-generated method stub
48             PagerFragment fragment=new PagerFragment();
49             Bundle bundle=new Bundle();
50             bundle.putString("TITLE", Titles[position]);
51             fragment.setArguments(bundle);
52             return fragment;
53         }
54 
55         @Override
56         public int getCount() {
57             // TODO Auto-generated method stub
58             return Titles.length;
59         }
60         @Override
61         public CharSequence getPageTitle(int position) {
62             // TODO Auto-generated method stub
63             return Titles[position];
64         }
65         
66     }
67 
68 }

 

fragment类:

 1 public class PagerFragment extends Fragment {
 2 
 3     @Override
 4     public View onCreateView(LayoutInflater inflater, ViewGroup container,
 5             Bundle savedInstanceState) {
 6         // TODO Auto-generated method stub
 7         RelativeLayout layout=new RelativeLayout(getActivity());
 8         LayoutParams params=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
 9         layout.setLayoutParams(params);
10         TextView text=new TextView(getActivity());
11         text.setLayoutParams(params);
12         text.setGravity(Gravity.CENTER);
13         Bundle bundle=getArguments();
14         String title=bundle.getString("TITLE");
15         text.setText(title);
16         layout.addView(text);
17         return layout;
18     }
19 }

 

attr:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <resources>
 3 
 4     <declare-styleable name="PagerSlidingTabStrip">
 5         <attr name="pstsIndicatorColor" format="color" />
 6         <attr name="pstsUnderlineColor" format="color" />
 7         <attr name="pstsDividerColor" format="color" />
 8         <attr name="pstsIndicatorHeight" format="dimension" />
 9         <attr name="pstsUnderlineHeight" format="dimension" />
10         <attr name="pstsDividerPadding" format="dimension" />
11         <attr name="pstsTabPaddingLeftRight" format="dimension" />
12         <attr name="pstsScrollOffset" format="dimension" />
13         <attr name="pstsTabBackground" format="reference" />
14         <attr name="pstsShouldExpand" format="boolean" />
15         <attr name="pstsTextAllCaps" format="boolean" />
16     </declare-styleable>
17 
18 </resources>

 

 

 

 

 

简单的导航viewpager

标签:

原文地址:http://www.cnblogs.com/ware/p/4432786.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!