目录
一、摘要
每个APP基本都有TabHost控件导航效果,从大公司的APP到小公司的APP,可以说Tab选项卡是APP的基础版块,比如常见的新浪、QQ、微信、网易、搜狐、人人等等,实现的Tab效果一种是可以滑动切换,一种是点击切换,同时实现选项卡的方式主要有以下几种(针对个人使用情况):
二、继承TabActivity
新的应用程序推荐使用Fragment代替TabActivity,TabActivity可以继续使用在旧版本的设备中,它的使用方式,第一步:设置布局文件,使用TabHost控件,第二步:继承TabActivity,仿QQ底部导航效果:
布局文件固定的写法:
- <?xml version="1.0" encoding="utf-8"?>
- <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@android:id/tabhost"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <FrameLayout
- android:id="@android:id/tabcontent"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- <TabWidget
- android:id="@android:id/tabs"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true">
- </TabWidget>
- </LinearLayout>
- </TabHost>
继续TabActivity,初始化控件,为TabHost对象配置选项卡内容
- /**
- * 添加选项卡
- */
- private void initView(){
- mTabHost= (TabHost) findViewById(android.R.id.tabhost);
- mTabHost.setFocusable(true);//设置当前对象是否获取焦点
- mTabHost.setup();//使用findViewById(),在添加选项卡前,调用该方法
- mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("消息").setContent(new Intent(this,cn.teachcourse.MessageActivity.class)));
- mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("联系人").setContent(new Intent(this,cn.teachcourse.ContactsActivity.class)));
- mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("动态").setContent(new Intent(this,cn.teachcourse.DynamicActivity.class)));
- }
TabHost方法使用说明:
setup()——>使用findViewById()方法获得TabHost对象,需要在addTab()方法前调用setup()方法
newTabSpec(String tag)——>创建TabHost.TabSpec对象,调用newTabSpec(String),tag标签区别不同的选项卡,通常不要重复就行
更多TabHost方法
addTab(TabHost.TabSpec tabSpec)——>添加一个选项卡
clearAllTabs()——>从TabWidget控件中删除与TabHost相关的选项卡
dispatchkeyEvent(KeyEvent event)——>获取焦点调度事件切换下一个视图
dispatchWindowFocusChanged(boolean hasFocus)——>window窗体包含当前视图,获得或失去焦点回调
getAccessibilityClassName()——>返回当前对象可访问的类名称
getCurrentTab()——>获得当前tab
getCurrentTabTag()——>获得当前tab的tag标签
getCurrentTabView()——>获得当前tab的View对象
getCurrentView()——>获得当前tab
getTabContentView()——>返回FrameLayout持有的tabcontent
getTabWidget()——>返回TabWidget对象
onTouchModeChanged(boolean isInTouchMode)——>回调方法,在touch mode改变时调用
setCurrentTab(int index)
setCurrentTabByTag(String tag)
setOnTabChangedListener(TabHost.OnTabChangeListener l)——>注册回调方法,在选项卡选择状态发生改变时调用
setup(LocalActivityManager activityGroup)——>调用setContent(Intent),必须调用该方法加载本地activity
TabHost.TabSpec方法使用说明:
setIndicator(CharSequence label)——>设置选项卡显示的文字信息,如上面的QQ选项卡的“消息”、“联系人”、“动态”
setContent(Intent intent)——>设置选项卡切换显示的Activity
更多TabHost.TabSpec方法
getTag()
setContent(int viewId)——>指定viewId,被用为tab content
setContent(TabHost.TabContentFactory contentFactory)——>指定TabHost.TabContentFactory创建tab content
setIndicator(View view)——>指定view,作为tab导航
setIndicator(CharSequence label,Drawable icon)——>指定标签、图标,作为tab导航
通常APP的导航的效果,上面显示图片,下面显示文字,可以使用setIndicator(View)加载自定义布局,当然也可以使用setIndicator(CharSequence,Drawable)直接指定文字和图标
三、继承FragmentActivity
使用FragmentTabHost代替TabHost,创建的activity需要继承FragmentActivity,相比TabHost布局文件,FragmentTabHost多一个FrameLayout控件,设置实际的内容,布局代码:
- <android.support.v4.app.FragmentTabHost
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@android:id/tabhost"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TabWidget
- android:id="@android:id/tabs"
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="0"/>
- <FrameLayout
- android:id="@android:id/tabcontent"
- android:layout_width="0dp"
- android:layout_height="0dp"
- android:layout_weight="0"/>
- <FrameLayout
- android:id="@+id/realtabcontent"
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="1"/>
- </LinearLayout>
- </android.support.v4.app.FragmentTabHost>
继承FragmentActivity中添加初始化控件代码,配置tab导航数据,相比TabActivity,使用Fragment代替Activity,代码如下:
- /**
- * 添加选项卡
- */
- private void initView(){
- mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
- mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);//初始化层次结构时,必须设置setup()方法
- mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("消息"),MessageFragment.class, null);
- mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("联系人"),ContactsFragment.class, null);
- mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("动态"),DynamicFragment.class, null);
- }
需要注意一点,FragmentTabHost继承了TabHost,具备父类的属性和方法,同时重写父类方法或添加自身特有的方法,比如:addTab(TabHost.TabSpec tabSpec,Class> clss,Bundle args),方便使用。
四、RadioButton+RadioGroup完美搭配
之前写过一篇文章《如何使用RadioGroup和RadioButton实现FragmentTabHost导航效果?》,简单介绍怎么去掉RadioButton默认属性,添加图标,设置选择时切换不同Activity或Fragment效果,最终的目的实现类似上文介绍的TabActivity、FragmentActivity实现的选项卡功能,似乎相比较,RadioButton+RadioGroup更加完美。
五、PagerSlidingTabStrip+ViewPager框架
在以前的文章里面有演示过PagerSlidingTabStrip+ViewPager实现新闻APP效果,PagerSlidingTabStrip一个封装好的控件,类似于TabWidget,使用的时候将PagerSlidingTabStrip作为自定义View复制当前项目中(包括各种属性),按照自定义View使用的方法在布局文件中引入PagerSlidingTabStrip和ViewPager,打造常见新闻APP导航效果。
关于PagerSlidingTabStrip
查看PagerSlidingTabStrip源码,知道PagerSlidingTabStrip继承HorizontalScrollView,重写HorizontalScrollView对应的方法,同时添加setViewPager(ViewPager vp)关联ViewPager,终点熟悉PagerSlidingTabStrip声明的属性:
- <declare-styleable name="PagerSlidingTabStrip">
- <attr name="pstsIndicatorColor" format="color" />
- <attr name="pstsUnderlineColor" format="color" />
- <attr name="pstsDividerColor" format="color" />
- <attr name="pstsCheckedTextColor" format="reference" />
- <attr name="pstsUncheckedTextColor" format="reference" />
- <attr name="pstsIndicatorHeight" format="dimension" />
- <attr name="pstsUnderlineHeight" format="dimension" />
- <attr name="pstsDividerPadding" format="dimension" />
- <attr name="pstsTabPaddingLeftRight" format="dimension" />
- <attr name="pstsScrollOffset" format="dimension" />
- <attr name="pstsTabBackground" format="reference" />
- <attr name="pstsShouldExpand" format="boolean" />
- <attr name="pstsTextAllCaps" format="boolean" />
- </declare-styleable>
上面的属性按理可以在布局文件中设置或代码设置都是可以的,怎么在自定义控件中引用自定义属性,可以参考《xml命名空间如何为自定义View取名?》按照上述字面上的英语意思比较好理解:
app:sptsIndicatorColor=""设置选项卡颜色
app:pstsUnderlineColor=""设置选项卡下划线颜色
app:pstsDividerColor=""设置选项卡之间间隔的颜色
app:pstsCheckedTextColor=""设置选项卡选中的颜色,可以参考默认颜色
app:pstsUncheckedTextColor=""设置选项卡默认的颜色,可以参考默认颜色
app:pstsIndicatorHeight=""设置选项卡的高度
app:pstsUnderlineHeight=""设置选项卡下划线的高度
app:pstsDividerPadding=""设置选项卡之间的内边距离
app:pstsTabPaddingLeftRight=""设置当前屏幕导航与左右边缘的之间距离
app:pstsScrollOffset=""设置选项卡滚动的偏移大小
app:pstsTabBackground=""设置导航背景颜色,可以参考默认颜色
app:pstsShouldExpand=""设置是否可以伸展,默认true,即导航可以左右滑动
app:pstsTextAllCaps=""设置导航文本是否大写,默认true
PagerSlidingTabStrip+ViewPager打造神奇导航
- <cn.teachcourse.viewpager.PagerSlidingTabStrip
- android:id="@+id/translate_pagertabstrip"
- xmlns:app="scheme.android.com/apk/res/cn.teachcourse.demoapp"
- android:layout_width="match_parent"
- android:layout_height="@dimen/dp_040"
- app:pstsIndicatorColor="@color/color_Pink"
- app:pstsUnderlineColor="@color/color_Crimson"
- app:pstsDividerColor="@color/color_LightGrey"
- app:pstsCheckedTextColor="@color/color_Crimson"
- app:pstsUncheckedTextColor="@color/color_DimGray"
- app:pstsIndicatorHeight="@dimen/dp_004"
- app:pstsUnderlineHeight="@dimen/dp_001"
- app:pstsShouldExpand="false"
- app:pstsTextAllCaps="true"
- android:layout_alignParentTop="true"/>
- <android.support.v4.view.ViewPager
- android:layout_below="@+id/translate_pagertabstrip"
- android:id="@+id/translate_viewpager"
- android:layout_width="match_parent"
- android:layout_height="match_parent"/>
在Activity中初始化布局,然后设置ViewPager适配器,添加PagerSlidingTabStrip和ViewPager之间的关联即可
- ViewPagerAdapter mAdapater=new ViewPagerAdapter(getSupportFragmentManager());
- mViewPager.setAdapter(mAdapater);
- mTabs.setViewPager(mViewPager);
- DisplayMetrics dm = getResources().getDisplayMetrics();
- mTabs.setTextSize((int) TypedValue.applyDimension(
- TypedValue.COMPLEX_UNIT_SP, 16, dm));
接下来的一篇文章会介绍Tab之间切换添加一些颜色渐变或动画效果,类似微信选项卡切换颜色渐变或类似新闻新闻下划线由短变长的动画效果。
你可能感兴趣的文章
转载请注明出处: https://www.teachcourse.cn/1825.html ,谢谢支持!