如何自定义fragmenttabhost用法中某一个Tab的点击效果

fragmenttabhost fragment实现底部tab切换(烟台杰瑞教育android培训部原创)
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
fragmenttabhost fragment实现底部tab切换(烟台杰瑞教育android培训部原创)
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer--144.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口如何在FragmentTabHost添加tab后获取Fragment实例_百度知道
如何在FragmentTabHost添加tab后获取Fragment实例
提问者采纳
=null); i++){
// count://blog, getSupportFragmentManager(),
info.setBackgroundResource(mBgImgs[i]);Tab选项卡的文字
private String mTextviewArray[] = {&a&quot,BaseFragment.class, info/实例化TabHost对象;
for(int i = 0;
}其中MyFragmentTabHost参考http.csdn, null).TabSpec tabSpec.class,mit().TabSpec tabSpec = mTabH定义FragmentTabHost对象
private MyFragmentTabHost mTabH&#47.class.id,主要是detach和attach部分改为了hide和show然后在addTab(TabHost.fragment = F
&#47.tabhost);定义数组来存放Fragment界面
private Class fragmentArray[] = {BaseF
&#47, Bundle args)函数最后增加;&#47
资深电脑人
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁FragmentActivity+FragmentTabHost+Fragement替代TabActibvity+TabHost+Activity - 推酷
FragmentActivity+FragmentTabHost+Fragement替代TabActibvity+TabHost+Activity
自Android3.2之后,TabActibvity被弃用(Deprecated),取而代之的是FragmentActivity,因为Fragment比Activiy更灵活,消耗的资源更小,完全能够满足TabActivity的效果,所以直接替代之。原来的TabActibvity+TabHost+Activity那套还可以用,不过强烈建议改用FragmentActivity+FragmentTabHost+Fragement
FragmentTabHost用法:
1. 定义FragmentActivity的layout:
&view plaincopy
LinearLayout
xmlns:android
&/apk/res/android&
android:layout_width
&fill_parent&
android:layout_height
&fill_parent&
android:orientation
&vertical&
FrameLayout
android:id
&@+id/realtabcontent&
android:layout_width
&fill_parent&
android:layout_height
android:layout_weight
android.support.v4.app.FragmentTabHost
android:id
&@android:id/tabhost&
android:layout_width
&fill_parent&
android:layout_height
&wrap_content&
android:background
&@drawable/maintab_toolbar_bg&
FrameLayout
&&&&&&&&&&&&
android:id
&@android:id/tabcontent&
&&&&&&&&&&&&
android:layout_width
&&&&&&&&&&&&
android:layout_height
&&&&&&&&&&&&
android:layout_weight
&&&&&&&&&&&&&&
android.support.v4.app.FragmentTabHost
LinearLayout
2. 必须继承FragmentActivity
&view plaincopy
&MainTabActivity&
&FragmentActivity{&&&&
//定义FragmentTabHost对象
&FragmentTabHost&mTabH&&
3. 得到FragmentTabHost对象
&view plaincopy
mTabHost&=&(FragmentTabHost)findViewById(android.R.id.tabhost);&&
4. 初始化FragmentTabHost对象
&view plaincopy
mTabHost.setup(
,&getSupportFragmentManager(),&R.id.realtabcontent);&&&
注意,这里的R.id.realtabcontent可以是任一个ViewGroup或其子类的对象id,比如LinearLayout。其实际作用就是个容器,Tab切换时,当前Tab对应的Fragment会被加入到这个ViewGroup作为其子View
5.按顺序添加每个Tab页
&view plaincopy
//为每一个Tab按钮设置图标、文字和内容
TabSpec&tabSpec&=&mTabHost.newTabSpec(
).setIndicator(yourTabItemView);&&&
//将Tab按钮添加进Tab选项卡中&
mTabHost.addTab(tabSpec,&fragmentPage1.
//设置Tab按钮的背景&
mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.selector_tab_background);&&
注意,mTabHost.newTabSpec(&TAG1&).setIndicator(yourTabItemview);这里的&TAG1&其实没什么什么意思,区分一下每个tab就好。
重点在于setIndicator函数,其有三个不同的实现,也就是说,你可以使用三种方式来定义你的Tab的风格:
&view plaincopy
//只使用要文字标识tab&
TabHost.TabSpec.setIndicator(CharSequence&label)&&&&
//使用文字+icon标识tab&
TabHost.TabSpec.setIndicator(CharSequence&label,&Drawable&icon)&&&
//使用自定义的View表示tab&
TabHost.TabSpec.setIndicator(View&view)&&
前面两种tab风格,是我们在绝大多数tabhost的范例中看到的风格(Holo风格),也就是当前选择的tab下面会有类似于滚动条的一个高亮显示的一个线条(indicator),很多时候我们不需要它,比如微信风格的Tab。这时候你就可以使用第三种方式来自定义你的Tab风格,你可以实现任何样式的Tab:
&view plaincopy
View&yourTabItemView&=&layoutInflater.inflate(R.layout.tab_item_view,&
ImageView&imageView&=&(ImageView)&view.findViewById(R.id.imageview);&&
imageView.setImageResource(mImageViewArray[index]);&&
TextView&textView&=&(TextView)&view.findViewById(R.id.textview);&&&&&&&&&&
textView.setText(mTextviewArray[index]);&&
另外,fragmentPage1.class是一个继承自Fragment的类,在切换Tab时,会被动态实例化,并且add到R.id.realtabcontent这个内容容器中显示
完成上面几点,一个简单的FragementActivity+FragmentTabHost+Fragment效果就出来了,接下来讲如何调整Tab停靠在顶部还是底部。
当R.id.realtabcontent与R.id.tabhost不在一个布局文件时,默认Tab在上TabContent在下,不能调整TabContent与Tab。
当R.id.realtabcontent与R.id.tabhost在一个布局文件时,如果R.id.realtabcontent在R.id.tabhost上面,那么Tab将会在TabContent下面,也就是说R.id.realtabcontent与R.id.tabhost的相对位置决定了选页在上还是在下。
不要在布局文件中给FragmentTabHost 设置子View,否则子View将显示在以Tab左上角为坐标0点的View中。给R.id.realtabcontent设置 android:layout_weight=&1&,因为默认时Tabcontent高度是wrap_content并且不能被调整,当Tab在Tabcontent下面并且显示的View不足把Tab挤到底部时,Tab会挂在显示的View的末尾,设置后Tabcontent就会被填充满了。
源码下载地址:
已发表评论数()
已收藏到推刊!
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
排版有问题
没有分页内容
视频无法显示
图片无法显示技术分享PARTICIPANTS STYLE
您当前的位置: & 技术分享
Android典型界面设计——FragmentTabHost+Fragment实现底部tab切换
&一、问题描述
  在上次博文中,我们使用RadioGroup+ViewPage+Fragmen实现了顶部滑动导航(查看文章:&),接下来我们使用FragmentTabHost+Fragment实现底部tab切换,效果如图所示
&二、案例主要组件
1、MainActivity布局
  把整个Activity分成两部TabHost和TabContent,TabHost包含各个tab,tab之间切换将在TabContent所关联的FrameLayout区域中显示各自板块的内容
复制内容到剪贴板
&&xmlns:android=&/apk/res/android&&&
&&&&xmlns:tools=&/tools&&&
&&&&android:layout_width=&match_parent&&&
&&&&android:layout_height=&match_parent&&&
&&&&android:orientation=&vertical&&&
&&&&tools:context=&.MainActivity&&&&
&&&&&&android:id=&@+id/contentLayout&&&
&&&&&&&&&android:layout_width=&match_parent&&&
&&&&&&&&android:layout_height=&0dp&&&
&&&&&&&&android:layout_weight=&1&&&&
&&&&&&&&&android:id=&@android:id/tabhost&&&
&&&&&&&&&android:layout_width=&match_parent&&&
&&&&&&&&&android:layout_height=&wrap_content&&&
&&&&&&&&&android:background=&#F6F6F6&&&
&&&&&&&&&&&
&&&&&&&&&&android:id=&@android:id/tabcontent&&&
&&&&&&&&&&&&&android:layout_height=&0dp&&android:layout_width=&0dp&&&
&&&&&&&&&&&&&&&
2、MainActivity代码
复制内容到剪贴板
public&class&MainActivity&extends&FragmentActivity&&
&implements&OnTabChangeListener{&&
&&&&private&FragmentTabHost&tabH&&
&&&&@Override&&
&&&&protected&void&onCreate(Bundle&savedInstanceState)&{&&
&&&&&&&&super.onCreate(savedInstanceState);&&
&&&&&&&&setContentView(R.layout.activity_main);&&
&&&&&&&&tabHost=(FragmentTabHost)super.findViewById(android.R.id.tabhost);&&
&&&&&&&&tabHost.setup(this,super.getSupportFragmentManager()&&
&&&&&&&&&&&&&&&&,R.id.contentLayout);&&
&&&&&&&&tabHost.getTabWidget().setDividerDrawable(null);&&
&&&&&&&&tabHost.setOnTabChangedListener(this);&&
&&&&&&&&initTab();&&
&&&&private&void&initTab(){&&
&&&&&&&&String&tabs[]=TabDb.getTabsTxt();&&
&&&&&&&&for(int&i=0;i&tabs.i++){&&
&&&&&&&&&&&&TabSpec&tabSpec=tabHost.newTabSpec(tabs[i]).setIndicator(getTabView(i));&&
&&&&&&&&&&&&tabHost.addTab(tabSpec,TabDb.getFragments()[i],null);&&
&&&&&&&&&&&&tabHost.setTag(i);&&
&&&&&&&&}&&
&&&&private&View&getTabView(int&idx){&&
&&&&&&&&View&view=LayoutInflater.from(this).inflate(R.layout.footer_tabs,null);&&
&&&&&&&&((TextView)view.findViewById(R.id.tvTab)).setText(TabDb.getTabsTxt()[idx]);&&
&&&&&&&&if(idx==0){&&
&&&&&&&&&&&&((TextView)view.findViewById(R.id.tvTab)).setTextColor(Color.RED);&&
&&&&((ImageView)view.findViewById(R.id.ivImg)).setImageResource(TabDb.getTabsImgLight()[idx]);&&
&&&&&&&&}else{&&
&&&&&&&&&&&&((ImageView)view.findViewById(R.id.ivImg)).setImageResource(TabDb.getTabsImg()[idx]);&&
&&&&&&&&}&&
&&&&&&&&return&&&
&&&&@Override&&
&&&&public&boolean&onCreateOptionsMenu(Menu&menu)&{&&
&&&&&&&&&&
&&&&&&&&getMenuInflater().inflate(R.menu.main,&menu);&&
&&&&&&&&return&true;&&
&&&&@Override&&
&&&&public&void&onTabChanged(String&tabId)&{&&
&&&&&&&&&&
&&&&&&&&updateTab();&&
&&&&private&void&updateTab(){&&
&&&&&&&&TabWidget&tabw=tabHost.getTabWidget();&&
&&&&&&&&for(int&i=0;i&tabw.getChildCount();i++){&&
&&&&&&&&&&&&View&view=tabw.getChildAt(i);&&
&&&&&&&&&&&&ImageView&iv=(ImageView)view.findViewById(R.id.ivImg);&&
&&&&&&&&&&&&if(i==tabHost.getCurrentTab()){&&
&&&&&&&&&&&&&&&&((TextView)view.findViewById(R.id.tvTab)).setTextColor(Color.RED);&&
&&&&&&&&&&&&&&&&iv.setImageResource(TabDb.getTabsImgLight()[i]);&&
&&&&&&&&&&&&}else{&&&&&&&&((TextView)view.findViewById(R.id.tvTab)).setTextColor(getResources().getColor(R.color.foot_txt_gray));&&
&&&&&&&&&&&&&&&&iv.setImageResource(TabDb.getTabsImg()[i]);&&
&&&&&&&&&&&&}&&
&&&&&&&&&&&&&&
&&&&&&&&}&&
3、TabDb组件
复制内容到剪贴板
public&class&TabDb&{&&
&&&&public&static&String[]&getTabsTxt(){&&
&&&&&&&&String[]&tabs={&新闻&,&阅读&,&试听&,&发现&,&&我&};&&
&&&&&&&&return&&&
&&&&public&static&int[]&getTabsImg(){&&
&&&&&&&&int[]&ids={R.drawable.foot_news_normal,R.drawable.foot_read_normal,R.drawable.foot_vdio_normal,R.drawable.foot_fond_normal,R.drawable.foot_out_normal};&&
&&&&&&&&return&&&
&&&&public&static&int[]&getTabsImgLight(){&&
&&&&&&&&int[]&ids={R.drawable.foot_news_light,R.drawable.foot_read_light,R.drawable.foot_vdio_light,R.drawable.foot_found_light,R.drawable.foot_out_light};&&
&&&&&&&&return&&&
&&&&public&static&Class[]&getFragments(){&&
&&&&&&&&Class[]&clz={NewsFragment.class,ReadFragment.class,VideoFragment.class,FoundFragment.class,OwnerFragment.class};&&
&&&&&&&&return&&&
4、每个tab各自对应的Fragment组件
  共5个Fragment为NewsFragment、ReadFragment、FoundFragment、OwnerFragment、VideoFragment,根据不同板块各自设计界面,这里重点是如何实现底部tab切换,简单布局一下即可,以NewsFragment为例代码如下:
复制内容到剪贴板
public&class&NewsFragment&extends&Fragment&{&&
&&&&@Override&&
&&&&public&void&onAttach(Activity&activity)&{&&
&&&&&&&&&&
&&&&&&&&super.onAttach(activity);&&
&&&&@Override&&
&&&&public&View&onCreateView(LayoutInflater&inflater,&ViewGroup&container,&&
&&&&&&&&&&&&Bundle&savedInstanceState)&{&&
&&&&&&&&&&
&&&&&&&&TextView&tvTitle=new&TextView(super.getActivity());&&
&&&&&&&&tvTitle.setText(&新闻&);&&
&&&&&&&&tvTitle.setLayoutParams(new&LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));&&
&&&&&&&&tvTitle.setGravity(Gravity.CENTER);&&
&&&&&&&&tvTitle.setTextSize(30);&&
&&&&&&&&return&tvT&&
&&&&@Override&&
&&&&public&void&setArguments(Bundle&args)&{&&
&&&&&&&&&&
&&&&&&&&super.setArguments(args);&&
5、tab布局样式(footer_tabs.xml)
复制内容到剪贴板
&version=&1.0&&encoding=&utf-8&&&
&xmlns:android=&/apk/res/android&&&
&&&&android:layout_width=&fill_parent&&&
&&&&android:layout_height=&wrap_content&&&&
&&&&android:gravity=&center&&&
&&&&android:padding=&5dp&&&
&&&&android:background=&#F6F6F6&&&
&&&&&&&&android:id=&@+id/ivImg&&&
&&&&&&&&android:layout_width=&wrap_content&&&
&&&&&&&&android:layout_height=&wrap_content&&&
&&&&&&&&android:layout_alignParentLeft=&true&&&
&&&&&&&&android:layout_alignParentTop=&true&&&
&&&&&&&&android:id=&@+id/tvTab&&&
&&&&&&&&android:layout_width=&wrap_content&&&
&&&&&&&&android:layout_height=&wrap_content&&&
&&&&&&&&android:layout_alignParentLeft=&true&&&
&&&&&&&&android:layout_below=&@+id/ivImg&&&
&&&&&&&&&&android:textColor=&#AEAEAE&&&
&&&&&&&&android:text=&新闻&&android:layout_marginTop=&2dp&&&
  想要了解更多内容的小伙伴,可以点击,亲自运行测试。
  疑问咨询或技术交流,请加入官方QQ群:&()
本文版权归所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。如何自定义FragmentTabHost中某一个Tab的点击效果_百度知道
如何自定义FragmentTabHost中某一个Tab的点击效果
提问者采纳
点击的位置tab改为点击后的背景选中某一个tab的时候设置一下他的背景图片,或者用一个int last记录上次点击的tab位置,点击后将上次的位置tab改为默认背景,然后将当前点击的tab位置赋给last ,然后将其他的tab都设置成默认背景
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁

我要回帖

更多关于 fragmenttabhost用法 的文章

 

随机推荐