FragmentTabHost FrameLayout實(shí)現(xiàn)底部導(dǎo)航欄
app經(jīng)常用到底部導(dǎo)航欄,早前使用過RadioGroup+FrameLayout實(shí)現(xiàn)或者RadioGroup+ViewPager實(shí)現(xiàn),現(xiàn)在基本使用FragmentTabHost+FrameLayout來(lái)實(shí)現(xiàn),因?yàn)槭褂闷饋?lái)簡(jiǎn)單易用。下面寫一個(gè)小例子簡(jiǎn)要地總結(jié)一下這個(gè)組合。
首先,看一下例子的最終運(yùn)行效果圖


這5個(gè)圖標(biāo)的效果其實(shí)都是一樣的,只要做出來(lái)一個(gè),以此類推就可以寫出其他幾個(gè)
第一步, 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代表是顯示內(nèi)容部分,下面的FragmentTabHost代表是導(dǎo)航欄部分。注意: FragmentTabHost的id和其內(nèi)部的FrameLayout的id必須是系統(tǒng)的id。
第二步, FragmentTabHost+FrameLayout代碼實(shí)現(xiàn)連接,F(xiàn)ragmentTabHost使用,可以記住三個(gè)步驟:(1)setup(…)可以理解為,初始化底部導(dǎo)航和內(nèi)容頁(yè)面連接,(2)新建TabSpec可以理解為初始化底部菜單項(xiàng),(3)addTab(…)可以理解為把菜單和內(nèi)容添加到控件中。下面看一下代碼:
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, 第三個(gè)參數(shù)為內(nèi)容容器
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
//第二步,初始化菜單項(xiàng)
TabHost.TabSpec tabSpec = mTabHost.newTabSpec("主頁(yè)");
/*添加菜單項(xiàng)布局*/
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("主頁(yè)");
tabSpec.setIndicator(view);
//第三步,添加菜單項(xiàng)和內(nèi)容
mTabHost.addTab(tabSpec, HomeFragment.class, null);
// initTabHost();
}
}
其中涉及了菜單項(xiàng)布局tab_indicator.xml,內(nèi)容頁(yè)布局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>
寫完如上代碼的運(yùn)行效果如下:

可以看到一個(gè)菜單項(xiàng)已經(jīng)顯示出來(lái),照葫蘆畫瓢,我們就可以吧其他四個(gè)菜單項(xiàng)寫出來(lái),既然其他四項(xiàng)和這個(gè)代碼雷同,所以說(shuō)肯定有公共部分可以抽取出來(lái),減少代碼量和代碼整潔度。我們發(fā)現(xiàn),有三個(gè)變量隨著菜單變化的,如:菜單圖標(biāo),菜單名稱,菜單對(duì)應(yīng)的內(nèi)容。所以我們寫一個(gè)類封裝一下,代碼如下:
TabDataBean.java
public class TabDataBean {
private int tabName;
private int tabIcon;
private Class content; //對(duì)應(yīng)的內(nèi)容類
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;
}
}
有了這個(gè)實(shí)體類,我們就可以把上面的第一步和第二步驟抽取出來(lái)封裝一下了,代碼如下:
private void initTabHost() {
//初始化fTabHost, 第三個(gè)參數(shù)為內(nèi)容容器
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
/*初始化數(shù)據(jù)源*/
TabDataBean bean = new TabDataBean(R.string.tabHome, R.drawable.tab_home_normal, HomeFragment.class);
//添加底部菜單項(xiàng)-tabSpec
TabHost.TabSpec tabSpec = mTabHost.newTabSpec(getString(bean.getTabName()));
//給菜單項(xiàng)添加內(nèi)容,indicator,其中indicator需要的參數(shù)View即為菜單項(xiàng)的布局
tabSpec.setIndicator(getIndicatorView(bean));
//第二參數(shù)就是該菜單項(xiàng)對(duì)應(yīng)的頁(yè)面內(nèi)容
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;
}
把其他四項(xiàng)添加入后,代碼如下:
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();
}
/**
* 初始化底部導(dǎo)航欄
*/
private void initTabHost() {
//初始化fTabHost, 第三個(gè)參數(shù)為內(nèi)容容器
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
/*初始化數(shù)據(jù)源*/
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);
//添加底部菜單項(xiàng)-tabSpec
for (TabDataBean bean : tabDataList) {
TabHost.TabSpec tabSpec = mTabHost.newTabSpec(getString(bean.getTabName()));
//給菜單項(xiàng)添加內(nèi)容,indicator,其中indicator需要的參數(shù)View即為菜單項(xiàng)的布局
tabSpec.setIndicator(getIndicatorView(bean));
//第二參數(shù)就是該菜單項(xiàng)對(duì)應(yīng)的頁(yè)面內(nèi)容
mTabHost.addTab(tabSpec, bean.getContent(), null);
}
}
/**
* 初始化indciator的內(nèi)容
* @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;
}
}
運(yùn)行效果如下:


如上結(jié)果,已經(jīng)離我們的目標(biāo)很近了。
第三步,給圖標(biāo)和文字添加變色selector
首先,給圖標(biāo)變色,在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>
接下來(lái)把
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);
以此類推,剩下的四項(xiàng)也是如此處理
然后,菜單名稱變色,如果在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>
接下來(lái)把tab_indicator.xml文件中TextView的Android:textColor="@color/tabTextColor" 修改為
android:textColor="@color/selector_tab_text"
最后運(yùn)行一下就和文章開頭的運(yùn)行效果一致了,有疑問或者是文章有不對(duì)的地方歡迎評(píng)論和指正^_^。
問題: 我們?cè)诿總€(gè)fragment的onActivityCreated(…)方法中都寫了
Toast.makeText(getContext(), R.string.tabHome, Toast.LENGTH_SHORT).show();
運(yùn)行程序,你會(huì)發(fā)現(xiàn),無(wú)論是第一次點(diǎn)擊還是再次進(jìn)入此菜單項(xiàng)時(shí),都會(huì)彈出toast對(duì)話框。如果我們?cè)诿總€(gè)頁(yè)面中都寫入了網(wǎng)絡(luò)請(qǐng)求,相當(dāng)于每次進(jìn)入都會(huì)進(jìn)行一次請(qǐng)求。但是項(xiàng)目需求只要求我們第一進(jìn)入該頁(yè)面時(shí)請(qǐng)求,所以我們應(yīng)該如何處理呢?有幾種處理方式,大家可以思考一下,下一篇文章,我們重寫FragmentTabHost來(lái)處理這個(gè)問題。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android6.0來(lái)電號(hào)碼與電話薄聯(lián)系人進(jìn)行匹配
這篇文章主要為大家詳細(xì)介紹了Android6.0來(lái)電號(hào)碼與電話薄聯(lián)系人進(jìn)行匹配的方法,感興趣的小伙伴們可以參考一下2016-07-07
Android中定時(shí)執(zhí)行任務(wù)的3種實(shí)現(xiàn)方法(推薦)
下面小編就為大家?guī)?lái)一篇Android中定時(shí)執(zhí)行任務(wù)的3種實(shí)現(xiàn)方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2016-11-11
android 仿微信demo——微信消息界面實(shí)現(xiàn)(服務(wù)端)
本系列文章主要介紹了微信小程序-閱讀小程序?qū)嵗╠emo),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧,希望能給你們提供幫助2021-06-06
Android Native 內(nèi)存泄漏系統(tǒng)化解決方案
這篇文章主要介紹了Android Native 內(nèi)存泄漏系統(tǒng)化解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Android實(shí)現(xiàn)兩圓點(diǎn)之間來(lái)回移動(dòng)加載進(jìn)度
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)兩圓點(diǎn)之間來(lái)回移動(dòng)加載進(jìn)度,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
Android編程實(shí)現(xiàn)的首頁(yè)左右滑動(dòng)切換功能示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)的首頁(yè)左右滑動(dòng)切換功能,涉及Android事件監(jiān)聽及響應(yīng)相關(guān)操作技巧,需要的朋友可以參考下2017-07-07
Android scrollview實(shí)現(xiàn)底部繼續(xù)拖動(dòng)查看圖文詳情
這篇文章主要為大家詳細(xì)介紹了Android scrollview實(shí)現(xiàn)底部繼續(xù)拖動(dòng)查看圖文詳情,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
基于android樣式與主題(style&theme)的詳解
本篇文章是對(duì)android中的樣式與主題(style&theme)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Android學(xué)習(xí)筆記——Menu介紹(二)
這次將繼續(xù)上一篇文章沒有講完的Menu的學(xué)習(xí),上下文菜單(Context menu)和彈出菜單(Popup menu)2014-10-10

