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來實(shí)現(xiàn),因?yàn)槭褂闷饋砗唵我子?。下面寫一個小例子簡要地總結(jié)一下這個組合。
首先,看一下例子的最終運(yùn)行效果圖
這5個圖標(biāo)的效果其實(shí)都是一樣的,只要做出來一個,以此類推就可以寫出其他幾個
第一步, 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使用,可以記住三個步驟:(1)setup(…)可以理解為,初始化底部導(dǎo)航和內(nèi)容頁面連接,(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, 第三個參數(shù)為內(nèi)容容器 mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); //第二步,初始化菜單項(xiàng) TabHost.TabSpec tabSpec = mTabHost.newTabSpec("主頁"); /*添加菜單項(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("主頁"); tabSpec.setIndicator(view); //第三步,添加菜單項(xiàng)和內(nèi)容 mTabHost.addTab(tabSpec, HomeFragment.class, null); // initTabHost(); } }
其中涉及了菜單項(xiàng)布局tab_indicator.xml,內(nèi)容頁布局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)行效果如下:
可以看到一個菜單項(xiàng)已經(jīng)顯示出來,照葫蘆畫瓢,我們就可以吧其他四個菜單項(xiàng)寫出來,既然其他四項(xiàng)和這個代碼雷同,所以說肯定有公共部分可以抽取出來,減少代碼量和代碼整潔度。我們發(fā)現(xiàn),有三個變量隨著菜單變化的,如:菜單圖標(biāo),菜單名稱,菜單對應(yīng)的內(nèi)容。所以我們寫一個類封裝一下,代碼如下:
TabDataBean.java
public class TabDataBean { private int tabName; private int tabIcon; private Class content; //對應(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; } }
有了這個實(shí)體類,我們就可以把上面的第一步和第二步驟抽取出來封裝一下了,代碼如下:
private void initTabHost() { //初始化fTabHost, 第三個參數(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)對應(yīng)的頁面內(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, 第三個參數(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)對應(yīng)的頁面內(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>
接下來把
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>
接下來把tab_indicator.xml文件中TextView的Android:textColor="@color/tabTextColor" 修改為
android:textColor="@color/selector_tab_text"
最后運(yùn)行一下就和文章開頭的運(yùn)行效果一致了,有疑問或者是文章有不對的地方歡迎評論和指正^_^。
問題: 我們在每個fragment的onActivityCreated(…)方法中都寫了
Toast.makeText(getContext(), R.string.tabHome, Toast.LENGTH_SHORT).show();
運(yùn)行程序,你會發(fā)現(xiàn),無論是第一次點(diǎn)擊還是再次進(jìn)入此菜單項(xiàng)時,都會彈出toast對話框。如果我們在每個頁面中都寫入了網(wǎng)絡(luò)請求,相當(dāng)于每次進(jìn)入都會進(jìn)行一次請求。但是項(xiàng)目需求只要求我們第一進(jìn)入該頁面時請求,所以我們應(yīng)該如何處理呢?有幾種處理方式,大家可以思考一下,下一篇文章,我們重寫FragmentTabHost來處理這個問題。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android 中ProgressDialog實(shí)現(xiàn)全屏效果的示例
本篇文章主要介紹了android 中ProgressDialog實(shí)現(xiàn)全屏效果的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11android二級listview列表實(shí)現(xiàn)代碼
今天來實(shí)現(xiàn)以下大眾點(diǎn)評客戶端的橫向listview二級列表,感興趣的朋友可以研究下2013-01-01Android圖片翻轉(zhuǎn)動畫簡易實(shí)現(xiàn)代碼
Android圖片翻轉(zhuǎn)動畫效果如何實(shí)現(xiàn),本文將給你一個驚喜,實(shí)現(xiàn)代碼已經(jīng)列出,需要的朋友可以參考下2012-11-11功能強(qiáng)大的Android滾動控件RecyclerView
這篇文章主要為大家詳細(xì)介紹了功能強(qiáng)大的Android滾動控件RecyclerView,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08詳解用RxJava實(shí)現(xiàn)事件總線(Event Bus)
本篇文章主要介紹了用RxJava實(shí)現(xiàn)事件總線(Event Bus),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11