Android 各种实现Tab效果的实现方式

一、TabActivity + TabWidget + TabHost.

实现TAB类型界面,首先想到的就是这种方式。但是在API level 13之后官方就不建议使用它了。不过还是在这里简单说一下它的使用吧。

使用它的关键就是布局文件了。需要在布局中添加<TabHost>、<TabWidget>、<FrameLayout>这三个控件,id分别是系统提供的:@android:id/tabhost 、@android:id/tabs 、@android:id/tabcontent 。

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=""android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TabHostandroid:id="@android:id/tabhost"android:layout_width="match_parent"android:layout_height="match_parent" ><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><!– 可以指定tabwidget的位置 android:layout_alignParentBottom="true" –><TabWidgetandroid:id="@android:id/tabs"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="false" ></TabWidget><FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_below="@android:id/tabs" ><LinearLayoutandroid:id="@+id/tab1"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#DEB887"android:orientation="vertical" ></LinearLayout><LinearLayoutandroid:id="@+id/tab2"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#BCEE68"android:orientation="vertical" ></LinearLayout><LinearLayoutandroid:id="@+id/tab3"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#7D9EC0"android:orientation="vertical" ></LinearLayout></FrameLayout></RelativeLayout></TabHost></LinearLayout>

一个linearlayout对应一个tab页面的布局。

tabHost = getTabHost();tabHost.addTab(tabHost.newTabSpec("111").setIndicator("", getResources().getDrawable(R.drawable.wuyong)).setContent(R.id.tab1));tabHost.addTab(tabHost.newTabSpec("222").setIndicator("",getResources().getDrawable(R.drawable.gongsunsheng)).setContent(R.id.tab2));tabHost.addTab(tabHost.newTabSpec("333").setIndicator("", getResources().getDrawable(R.drawable.likui)).setContent(R.id.tab3));tabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));tabHost.setCurrentTab(0);tabHost.setOnTabChangedListener(new OnTabChangeListener() {@Overridepublic void onTabChanged(String tabId) {Toast.makeText(FourthActivity.this, tabId, Toast.LENGTH_SHORT).show();}});二、ViewPager + PageAdapter

目前最常见的tab界面就是使用viewpager来实现了。

先来说一下viewpager的一般使用步骤:

1. 在布局文件中添加viewpager控件

2. 在代码中设置viewpager适配器,该类继承与pagerAdapter或它的子类。必须实现以下四个方法:

(1)getCount()

(2)instantiateItem()

(3)destroyItem()

(4)isViewFromObject()

3. 初始化viewpager控件,设置监听器

4. 设置监听事件(setOnPageChangeListener)

下面看一下这种方式的效果图:

主要的功能代码如下:

private void init() {viewPager = (ViewPager) findViewById(R.id.first_vp);LayoutInflater inflater = LayoutInflater.from(this);View view1 = inflater.inflate(R.layout.first_layout1, null);View view2 = inflater.inflate(R.layout.first_layout2, null);View view3 = inflater.inflate(R.layout.first_layout3, null);list.add(view1);list.add(view2);list.add(view3);viewPager.setAdapter(pagerAdapter);viewPager.setOnPageChangeListener(new OnPageChangeListener() {@Overridepublic void onPageSelected(int arg0) {setDots(arg0);}@Overridepublic void onPageScrolled(int arg0, float arg1, int arg2) {}@Overridepublic void onPageScrollStateChanged(int arg0) {}});}private PagerAdapter pagerAdapter = new PagerAdapter() { //官方建议这么写@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {return arg0 == arg1;} //返回一共有多少个界面@Overridepublic int getCount() {return list.size();} //实例化一个item@Overridepublic Object instantiateItem(ViewGroup container, int position) {container.addView(list.get(position));return list.get(position);} //销毁一个item@Overridepublic void destroyItem(ViewGroup container, int position, Object object) {container.removeView(list.get(position));}};

适配器中必须要实现以上的四个方法。

风景如何,其实并不重要。重要的是,你在我的身边。

Android 各种实现Tab效果的实现方式

相关文章:

你感兴趣的文章:

标签云: