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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
布局大体分为两部分,上面的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);@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mInflater = LayoutInflater.from(this);mTabHost = (FragmentTabHost) this.findViewById(android.R.id.tabhost);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);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
其中涉及了菜单项布局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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
HomeFragment.java
public class HomeFragment extends Fragment {
@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {return inflater.inflate(R.layout.fragment_home, null);}@Overridepublic void onActivityCreated(@Nullable Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);Toast.makeText(getContext(), R.string.tabHome, Toast.LENGTH_SHORT).show();}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
写完如上代码的运行效果如下:
可以看到一个菜单项已经显示出来,照葫芦画瓢,我们就可以吧其他四个菜单项写出来,既然其他四项和这个代码雷同,所以说肯定有公共部分可以抽取出来,减少代码量和代码整洁度。我们发现,有三个变量随着菜单变化的,如:菜单图标,菜单名称,菜单对应的内容。所以我们写一个类封装一下,代码如下:
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;}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
有了这个实体类,我们就可以把上面的第一步和第二步骤抽取出来封装一下了,代码如下:
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)//添加底部菜单项-tabSpecTabHost.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}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
把其他四项添加入后,代码如下:
public class MainActivity extends AppCompatActivity {
private LayoutInflater mInflater;private FragmentTabHost mTabHost;private ArrayList<TabDataBean> tabDataList = new ArrayList<>(5);@Overrideprotected 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() {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);for (TabDataBean bean : tabDataList) {TabHost.TabSpec tabSpec = mTabHost.newTabSpec(getString(bean.getTabName()));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;}}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
运行效果如下:
—》
如上结果,已经离我们的目标很近了。
第三步, 给图标和文字添加变色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来处理这个问题。