FragmentTabHost FrameLayout实现底部导航栏

app经常用到底部导航栏,早前使用过RadioGroup+FrameLayout实现或者RadioGroup+ViewPager实现,现在基本使用FragmentTabHost+FrameLayout来实现,因为使用起来简单易用。下面写一个小例子简要地总结一下这个组合。

首先,看一下例子的最终运行效果图

这5个图标的效果其实都是一样的,只要做出来一个,以此类推就可以写出其他几个

第一步, FragmentTabHost+FrameLayout布局,先看一下代码:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 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="0dp" android:layout_weight="1" android:background="@color/bg_color"/> <android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0" /> </android.support.v4.app.FragmentTabHost> </LinearLayout>

布局大体分为两部分,上面的FrameLayout代表是显示内容部分,下面的FragmentTabHost代表是导航栏部分。注意: FragmentTabHost的id和其内部的FrameLayout的id必须是系统的id。

第二步, FragmentTabHost+FrameLayout代码实现连接,FragmentTabHost使用,可以记住三个步骤:(1)setup(…)可以理解为,初始化底部导航和内容页面连接,(2)新建TabSpec可以理解为初始化底部菜单项,(3)addTab(…)可以理解为把菜单和内容添加到控件中。下面看一下代码:

public class MainActivity extends AppCompatActivity { private LayoutInflater mInflater; private FragmentTabHost mTabHost; private ArrayList<TabDataBean> tabDataList = new ArrayList<>(5); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mInflater = LayoutInflater.from(this); mTabHost = (FragmentTabHost) this.findViewById(android.R.id.tabhost); //第一步,初始化fTabHost, 第三个参数为内容容器 mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); //第二步,初始化菜单项 TabHost.TabSpec tabSpec = mTabHost.newTabSpec("主页"); /*添加菜单项布局*/ View view = mInflater.inflate(R.layout.tab_indicator, null); ImageView iconTab = (ImageView) view.findViewById(R.id.iv_tab_icon); TextView tvTab = (TextView) view.findViewById(R.id.tv_tab_text); iconTab.setImageResource(R.drawable.tab_home_normal); tvTab.setText("主页"); tabSpec.setIndicator(view); //第三步,添加菜单项和内容 mTabHost.addTab(tabSpec, HomeFragment.class, null); // initTabHost(); } }

其中涉及了菜单项布局tab_indicator.xml,内容页布局HomeFragment.java文件,代码如下:

tab_indicator.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="3dp" android:paddingBottom="3dp" android:layout_gravity="center" android:gravity="center"> <ImageView android:id="@+id/iv_tab_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_tab_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/tabTextColor" android:layout_marginTop="2dp"/> </LinearLayout>

HomeFragment.java

public class HomeFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_home, null); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Toast.makeText(getContext(), R.string.tabHome, Toast.LENGTH_SHORT).show(); } }

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/themeColor" android:text="@string/tabHome"/> </LinearLayout>

写完如上代码的运行效果如下:

可以看到一个菜单项已经显示出来,照葫芦画瓢,我们就可以吧其他四个菜单项写出来,既然其他四项和这个代码雷同,所以说肯定有公共部分可以抽取出来,减少代码量和代码整洁度。我们发现,有三个变量随着菜单变化的,如:菜单图标,菜单名称,菜单对应的内容。所以我们写一个类封装一下,代码如下:

TabDataBean.java

public class TabDataBean { private int tabName; private int tabIcon; private Class content; //对应的内容类 public TabDataBean(int tabName, int tabIcon, Class content) { this.tabName = tabName; this.tabIcon = tabIcon; this.content = content; } public int getTabName() { return tabName; } public void setTabName(int tabName) { this.tabName = tabName; } public int getTabIcon() { return tabIcon; } public void setTabIcon(int tabIcon) { this.tabIcon = tabIcon; } public Class getContent() { return content; } public void setContent(Class content) { this.content = content; } }

有了这个实体类,我们就可以把上面的第一步和第二步骤抽取出来封装一下了,代码如下:

private void initTabHost() { //初始化fTabHost, 第三个参数为内容容器 mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); /*初始化数据源*/ TabDataBean bean = new TabDataBean(R.string.tabHome, R.drawable.tab_home_normal, HomeFragment.class); //添加底部菜单项-tabSpec TabHost.TabSpec tabSpec = mTabHost.newTabSpec(getString(bean.getTabName())); //给菜单项添加内容,indicator,其中indicator需要的参数View即为菜单项的布局 tabSpec.setIndicator(getIndicatorView(bean)); //第二参数就是该菜单项对应的页面内容 mTabHost.addTab(tabSpec, bean.getContent(), null) } private View getIndicatorView(TabDataBean bean){ View view = mInflater.inflate(R.layout.tab_indicator, null); ImageView iconTab = (ImageView) view.findViewById(R.id.iv_tab_icon); TextView tvTab = (TextView) view.findViewById(R.id.tv_tab_text); iconTab.setImageResource(bean.getTabIcon()); tvTab.setText(bean.getTabName()); return view; }

把其他四项添加入后,代码如下:

public class MainActivity extends AppCompatActivity { private LayoutInflater mInflater; private FragmentTabHost mTabHost; private ArrayList<TabDataBean> tabDataList = new ArrayList<>(5); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mInflater = LayoutInflater.from(this); mTabHost = (FragmentTabHost) this.findViewById(android.R.id.tabhost); initTabHost(); } /** * 初始化底部导航栏 */ private void initTabHost() { //初始化fTabHost, 第三个参数为内容容器 mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); /*初始化数据源*/ TabDataBean tabHome = new TabDataBean(R.string.tabHome, R.drawable.tab_home_normal, HomeFragment.class); TabDataBean tabHot = new TabDataBean(R.string.tabHot, R.drawable.tab_life_normal, HotFragment.class); TabDataBean tabCategory = new TabDataBean(R.string.tabCategory, R.drawable.tab_service_normal, CategoryFragment.class); TabDataBean tabCart = new TabDataBean(R.string.tabCart, R.drawable.tab_order_normal, CartFragment.class); TabDataBean tabMine = new TabDataBean(R.string.tabMine, R.drawable.tab_mine_normal, MineFragment.class); tabDataList.add(tabHome); tabDataList.add(tabHot); tabDataList.add(tabCategory); tabDataList.add(tabCart); tabDataList.add(tabMine); //添加底部菜单项-tabSpec for (TabDataBean bean : tabDataList) { TabHost.TabSpec tabSpec = mTabHost.newTabSpec(getString(bean.getTabName())); //给菜单项添加内容,indicator,其中indicator需要的参数View即为菜单项的布局 tabSpec.setIndicator(getIndicatorView(bean)); //第二参数就是该菜单项对应的页面内容 mTabHost.addTab(tabSpec, bean.getContent(), null); } } /** * 初始化indciator的内容 * @param bean */ private View getIndicatorView(TabDataBean bean){ View view = mInflater.inflate(R.layout.tab_indicator, null); ImageView iconTab = (ImageView) view.findViewById(R.id.iv_tab_icon); TextView tvTab = (TextView) view.findViewById(R.id.tv_tab_text); iconTab.setImageResource(bean.getTabIcon()); tvTab.setText(bean.getTabName()); return view; } }

运行效果如下:

如上结果,已经离我们的目标很近了。

第三步,给图标和文字添加变色selector
首先,给图标变色,在drawable文件夹下新建selector_tab_home.xml,代码如下:

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:drawable="@drawable/tab_home_selected"/> <item android:state_pressed="true" android:drawable="@drawable/tab_home_selected"/> <item android:drawable="@drawable/tab_home_normal"/> </selector>

接下来把

TabDataBean tabHome = new TabDataBean(R.string.tabHome, R.drawable.tab_home_normal, HomeFragment.class); 改为
TabDataBean tabHome = new TabDataBean(R.string.tabHome, R.drawable.selector_tab_home, HomeFragment.class);
以此类推,剩下的四项也是如此处理

然后,菜单名称变色,如果在res文件夹下没有color资源文件夹,新建color资源文件夹,然后在color文件夹下新建selector_tab_text.xml文件,代码如下:

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@color/themeColor" android:state_selected="true"/> <item android:color="@color/themeColor" android:state_active="true"/> <item android:color="@color/tabTextColor" android:state_selected="false"/> <item android:color="@color/tabTextColor" android:state_active="false"/> </selector>

接下来把tab_indicator.xml文件中TextView的Android:textColor="@color/tabTextColor" 修改为

android:textColor="@color/selector_tab_text"

最后运行一下就和文章开头的运行效果一致了,有疑问或者是文章有不对的地方欢迎评论和指正^_^。

问题: 我们在每个fragment的onActivityCreated(…)方法中都写了

Toast.makeText(getContext(), R.string.tabHome, Toast.LENGTH_SHORT).show();

运行程序,你会发现,无论是第一次点击还是再次进入此菜单项时,都会弹出toast对话框。如果我们在每个页面中都写入了网络请求,相当于每次进入都会进行一次请求。但是项目需求只要求我们第一进入该页面时请求,所以我们应该如何处理呢?有几种处理方式,大家可以思考一下,下一篇文章,我们重写FragmentTabHost来处理这个问题。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

时间: 2024-08-19 08:23:55

FragmentTabHost FrameLayout实现底部导航栏的相关文章

TextView+Fragment实现底部导航栏

前言:项目第二版刚上线没多久,产品又对需求进行了大改动,以前用的是左滑菜单,现在又要换成底部导航栏,于是今天又苦逼加班了.花了几个小时实现了一个底部导航栏的demo,然后总结一下.写一篇博客.供自己以后参考.也可以给没有做过的朋友进行参考.以后大家有类似的功能就可以在我的demo上就行修改. 一.先上效果图:   本来是打算用FragmentTabHost实现的,但是中间那个按钮有点麻烦,想到我们项目好几个产品经理,并且经常改需求,于是最后决定  用 TextView+Fragment去实现. 

android-这个地底部导航栏是怎么做出来的

问题描述 这个地底部导航栏是怎么做出来的 首页,订单,我的三个按钮,主要是底部一直悬浮在那儿做导航按钮这个效果怎么弄的 解决方案 如上面说的你可以在下面用个Fragment,上面用FrameLayout,但是切换的不同界面都要继承自Fragment,而不是Activity.最近我也在写这个,我直接在界面上用FrameLayout布局,然后把要显示的不同界面的布局都叠加到FrameLayout中,然后点击不同的按钮可以把其他的界面隐藏掉,只显示一个界面 解决方案二: 你可以当下面是个framlay

三步搞定android应用底部导航栏

很多android应用底部都有一个底部导航栏,方便用户在使用过程中随意切换.目前常用的做法有三种:一种是使用自定义tabHost,一种是使用activityGroup,一种是结合FrameLayout实现.笔者再做了多款应用后,为了节约开发周期,封装了一个抽象类,只要三步便可完成底部栏的生成及不同页面的调用. public class ActivitycollectiondemoActivity extends ActivityCollection { /** Called when the a

Android底部导航栏实现(三)之TextView+LinearLayout

这里简单记录下通过TextView+LinearLayout+Fragment来实现Android底部导航栏. 布局 <!--fragment_text_tab.xml-->    <?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"       

Android程序开发之Fragment实现底部导航栏实例代码_Android

流行的应用的导航一般分为两种,一种是底部导航,一种是侧边栏. 说明 IDE:AS,Android studio; 模拟器:genymotion; 实现的效果,见下图. 具体实现 为了讲明白这个实现过程,我们贴出来的代码多一写,这样更方便理解 [最后还会放出完整的代码实现] .看上图的界面做的比较粗糙,但实现过程的骨架都具有了,想要更完美的设计,之后自行完善吧 ^0^. 布局 通过观察上述效果图,发现任意一个选项页面都有三部分组成: 顶部去除ActionBar后的标题栏: 中间一个Fragment

Android应用底部导航栏(选项卡)实例

  现在很多android的应用都采用底部导航栏的功能,这样可以使得用户在使用过程中随意切换不同的页面,现在我采用TabHost组件来自定义一个底部的导航栏的功能. 我们先看下该demo实例的框架图: 其中各个类的作用以及资源文件就不详细解释了,还有资源图片(在该Demo中借用了其它应用程序的资源图片)也不提供了,大家可以自行更换自己需要的资源图片.直接上各个布局文件或各个类的代码: [1]  res/layout目录下的 maintabs.xml 源码: [html] view plainco

【android中级】之Android应用底部导航栏(选项卡)实例

现在很多android的应用都采用底部导航栏的功能,这样可以使得用户在使用过程中随意切换不同的页面,现在我采用TabHost组件来自定义一个底部的导航栏的功能. 我们先看下该demo实例的框架图: 其中各个类的作用以及资源文件就不详细解释了,还有资源图片(在该Demo中借用了其它应用程序的资源图片)也不提供了,大家可以自行更换自己需要的资源图片www.lovewenzhang.com.直接上各个布局文件或各个类的代码: [1]  res/layout目录下的maintabs.xml : [htm

Android实现底部导航栏功能(选项卡)

现在很多android的应用都采用底部导航栏的功能,这样可以使得用户在使用过程中随意切换不同的页面,现在我采用TabHost组件来自定义一个底部的导航栏的功能. 我们先看下该demo实例的框架图: 其中各个类的作用以及资源文件就不详细解释了,还有资源图片(在该Demo中借用了其它应用程序的资源图片)也不提供了,大家可以自行更换自己需要的资源图片.直接上各个布局文件或各个类的代码: 1. res/layout目录下的 maintabs.xml 源码: <?xml version="1.0&q

Android程序开发之Fragment实现底部导航栏实例代码

流行的应用的导航一般分为两种,一种是底部导航,一种是侧边栏. 说明 IDE:AS,Android studio; 模拟器:genymotion; 实现的效果,见下图. 具体实现 为了讲明白这个实现过程,我们贴出来的代码多一写,这样更方便理解 [最后还会放出完整的代码实现] .看上图的界面做的比较粗糙,但实现过程的骨架都具有了,想要更完美的设计,之后自行完善吧 ^0^. 布局 通过观察上述效果图,发现任意一个选项页面都有三部分组成: 顶部去除ActionBar后的标题栏: 中间一个Fragment