PagerTabStrip和PagerTitleStrip

来源:互联网 发布:成都市软件行业协会 编辑:程序博客网 时间:2024/05/22 11:43

</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">

PagerTabStrip


PagerTitleStrip



PagerTabStrip和PagerTitleStrip通常都是作为PagerView和子控件添加在XML布局文件中,android:layout_gravity 属性设置为TOP或BOTTOM来将它显示在ViewPager的顶部或底部,每个页面的标题是通过适配器的getPageTitle(int)函数提供给ViewPager的

PagerTabStrip和PagerTitleStrip的区别在于

1.PagerTabStrip在当前页面下,会有一个下划线条来提示当前页面的Tab是哪个。

2.PagerTabStrip的Tab是可以点击的,当用户点击某一个Tab时,当前页面就会跳转到这个页面,而PagerTitleStrip则没这个功能。

PagerTitleStrip的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"    tools:context="com.example.pageview.MainActivity" >    <android.support.v4.view.ViewPager        android:id="@+id/viewPage"        android:layout_width="fill_parent"        android:layout_height="fill_parent" >        <android.support.v4.view.PagerTitleStrip              android:id="@+id/pagerTabStrip"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="top"             android:background="#ff0000"            />                    </android.support.v4.view.ViewPager></LinearLayout>

PagerTabStrip的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"    tools:context="com.example.pageview.MainActivity" >    <android.support.v4.view.ViewPager        android:id="@+id/viewPage"        android:layout_width="fill_parent"        android:layout_height="fill_parent" >        <android.support.v4.view.PagerTabStrip              android:id="@+id/pagerTabStrip"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="top"             android:background="#ff0000"            />                    </android.support.v4.view.ViewPager></LinearLayout>

<------------------------------------------------------------------------------------简单案例两个以上的使用只是以上的xml不同

frame_layout.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <ImageView     android:layout_height="fill_parent"android:layout_width="fill_parent"android:id="@+id/imageView"        /></FrameLayout>

MainActivity文件

public class MainActivity extends Activity {private ViewPager viewPager;private int drawables[] = {R.drawable.biz_ad_new_version1_img0,R.drawable.biz_ad_new_version1_img1,R.drawable.biz_ad_new_version1_img2,R.drawable.biz_ad_new_version1_img3};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);viewPager = (ViewPager) this.findViewById(R.id.viewPage);List<View> views = new ArrayList<View>();for(int i=0;i<drawables.length;i++){LayoutInflater inflater = getLayoutInflater().from(this);View view = inflater.inflate(R.layout.frame_layout, null);ImageView imageView = (ImageView) view.findViewById(R.id.imageView);imageView.setImageResource(drawables[i]);views.add(view);}viewPager.setAdapter(new MyAdapter2(this, views));viewPager.setCurrentItem(0);}}class MyAdapter2 extends PagerAdapter{ArrayList<View> views ;Context context;private List<String> titleList;public MyAdapter2(Context context,List list){this.views = (ArrayList<View>) list;this.context = context;titleList = new ArrayList<String>();titleList.add("注册页面1");titleList.add("注册页面2");titleList.add("注册页面3");titleList.add("注册页面4");}@Overridepublic int getCount() {return views.size();}@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {// TODO Auto-generated method stubreturn arg0 == (arg1);}@Overridepublic void destroyItem(ViewGroup container, int position, Object object) {container.removeView(views.get(position));System.out.println("--------------------remove position"+position);}@Overridepublic Object instantiateItem(ViewGroup container, int position) {container.addView(views.get(position));System.out.println("--------------------add position"+position);return views.get(position);}@Overridepublic CharSequence getPageTitle(int position) {// TODO Auto-generated method stubreturn titleList.get(position);} }}
图片自加....

二.PagerTabStrip添加图片改变样式

@Override  public CharSequence getPageTitle(int position) {        SpannableStringBuilder ssb = new SpannableStringBuilder("  "+titleList.get(position)); // space added before text                                          // for      Drawable myDrawable = getResources().getDrawable(              R.drawable.ic_launcher);      myDrawable.setBounds(0, 0, myDrawable.getIntrinsicWidth(),              myDrawable.getIntrinsicHeight());      ImageSpan span = new ImageSpan(myDrawable,              ImageSpan.ALIGN_BASELINE);        ForegroundColorSpan fcs = new ForegroundColorSpan(Color.GREEN);// 字体颜色设置为绿色      ssb.setSpan(span, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);// 设置图标      ssb.setSpan(fcs, 1, ssb.length(),              Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);// 设置字体颜色      ssb.setSpan(new RelativeSizeSpan(1.2f), 1, ssb.length(),              Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);      return ssb;  }  

总结:个人观点.看起来非常丑应用中没有什么卵用

参考翻译学习感谢:http://blog.csdn.net/harvic880925/article/details/38521865

0 0