使用SlidingTabLayout需要准备2个类,分别是 SlidingTabLayout,与SlidingTabStrip,,放进项目中时只用修改下包名即可。
效果制作的不是很好。
这篇文章,也是在网上搜了很多资源参考,对 SlidingTabLayout.java和SlidingTabStrip.java进行了修改。大家可以更改他的格式字体大小、选中状态,分割线调整等等。先上传这两个文件,改动支出都做了注释。
SlidingTabLayout.java
/* * Copyright (C) 2013 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://patActivity { //创建 颜色数组 用来做viewpager的背景 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ViewPager pager = (ViewPager) findViewById(R.id.view_pager); SlidingTabLayout tab = (SlidingTabLayout) findViewById(R.id.sliding); tab.setDividerColors(Color.TRANSPARENT); //设置标题的分割线 tab.setSelectedIndicatorColors(Color.rgb(51, 181, 229)); //设置滚动条的颜色 tab.setTitleSize(18); //...设置字体的颜色,默认16 MyAdapte adapter = new MyAdapte(); pager.setAdapter(adapter); tab.setViewPager(pager); } int[] colors = {0xFF123456, 0xFF654321, 0xFF336699}; class MyAdapte extends PagerAdapter { //可以考虑把这个数组添加到集合里面 String[] titles = {"AA", "BB", "CC"}; ArrayList<LinearLayout> layouts = new ArrayList<LinearLayout>(); MyAdapte() { for (int i = 0; i < 3; i++) { LinearLayout l = new LinearLayout(MainActivity.this); l.setBackgroundColor(colors[i]); l.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); layouts.add(l); } } @Override public int getCount() { return layouts.size(); } @Override public boolean isViewFromObject(View view, Object o) { return view == o; } @Override public Object instantiateItem(ViewGroup container, int position) { LinearLayout l = layouts.get(position); container.addView(l); return l; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(layouts.get(position)); } @Override public CharSequence getPageTitle(int position) { //...可以返回集合list.get(position); return titles[position]; } }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。