FragmentTabHost实现app底部tab功能
来源:互联网 发布:网络没问题lol连不上 编辑:程序博客网 时间:2024/06/05 21:00
Demo下载连接:点击打开链接
FragmentTabHost是继承之TabHost的 但是TabHost已经过时 FragmentTabHost是结合fragment一起使用的 在使用的过程中需要注意 Activity必须是继承之FragmentActivity的 否则会报错
使用FragmentTabHost主要流程步骤:
每一个tab通过enum进行定义 代码如下:
public enum MainTab { NEWS(0, R.string.main_tab_name_news, R.drawable.tab_icon_new, NewsViewPagerFragment.class), TWEET(1, R.string.main_tab_name_tweet, R.drawable.tab_icon_tweet, TweetsViewPagerFragment.class), ME(2, R.string.main_tab_name_my, R.drawable.tab_icon_me, MyInformationFragment.class); private int idx; private int resName; private int resIcon; private Class<?> clz; private MainTab(int idx, int resName, int resIcon, Class<?> clz) { this.idx = idx; this.resName = resName; this.resIcon = resIcon; this.clz = clz; } public int getIdx() { return idx; } public void setIdx(int idx) { this.idx = idx; } public int getResName() { return resName; } public void setResName(int resName) { this.resName = resName; } public int getResIcon() { return resIcon; } public void setResIcon(int resIcon) { this.resIcon = resIcon; } public Class<?> getClz() { return clz; } public void setClz(Class<?> clz) { this.clz = clz; }}
MainActivity的代码:
public class MainActivity extends AppCompatActivity { private FragmentTabHost mTabHost; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.inject(this); initView(); } private void initView() { mTabHost = (FragmentTabHost) findViewById(R.id.tabhost); mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); if (android.os.Build.VERSION.SDK_INT > 10) { //去除tab之间的竖线 mTabHost.getTabWidget().setShowDividers(0); } initTab(); } private void initTab() { MainTab[] tabs = MainTab.values(); for (int i = 0; i < tabs.length; i++) { MainTab mainTab = tabs[i]; TabHost.TabSpec tab = mTabHost.newTabSpec(getString(mainTab.getResName())); View indicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, null); TextView tabTitle = (TextView) indicator.findViewById(R.id.tab_title); tabTitle.setText(getString(mainTab.getResName())); tabTitle.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(mainTab.getResIcon()), null, null); tab.setIndicator(indicator); tab.setContent(new TabHost.TabContentFactory() { @Override public View createTabContent(String tag) { return new View(MainActivity.this); } }); mTabHost.addTab(tab, mainTab.getClz(), null); } }}
布局文件 activtiy_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <FrameLayout android:id="@+id/realtabcontent" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="4dip"> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#c8c8c8" /> <android.support.v4.app.FragmentTabHost android:id="@+id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="4dip" /> </RelativeLayout></LinearLayout>
0 0
- FragmentTabHost实现app底部tab功能
- FragmentTabHost实现底部tab切换
- 【Android】FragmentTabHost实现底部Tab菜单选项
- Android FragmentTabHost实现底部tab切换
- FragmentTabHost+ViewPager+Fragment实现底部Tab导航
- FragmentTabHost、Fragment实现底部点击切换Tab页面
- FragmentTabHost、Fragment实现底部点击切换Tab页面
- 利用Fragment和FragmentTabHost实现各类app底部菜单栏
- FragmentTabHost实现底部导航
- APP首页底部Tab
- Android典型界面设计——FragmentTabHost+Fragment实现底部tab切换
- 首页-底部Tab导航(菜单栏)的实现:FragmentTabHost+ViewPager+Fragment
- 首页-底部Tab导航(菜单栏)的实现:FragmentTabHost+ViewPager+Fragment
- FragmentTabHost+Fragment实现底部菜单栏
- Fragment+FragmentTabHost实现底部菜单栏
- 用 FragmentTabHost 实现底部菜单
- FragmentTabHost实现底部导航菜单
- 用FragmentTabHost 实现底部菜单
- 2015-12-7 项目2—用哈希法组织关键字
- 我对mysql的深入了解
- 第十五周 项目二(1) 用哈希法组织关键字
- 今日起暂更学习日志
- nginx 安装
- FragmentTabHost实现app底部tab功能
- 第十五周项目(2):用哈希法组织关键字1——线性探查法处理冲突
- 第十五周项目-B-树的基本操作
- 第14周项目1(1) 验证折半查找算法
- 第十五周--项目3B-树的基本操作
- 工作流学习(三)
- 第15周项目1-验证算法
- Android -wifi 直连(wifi direct )
- 黑马程序员——OC之类方法与set、get方法