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>
寫完如上代碼的運行效果如下:
可以看到一個菜單項已經顯示出來,照葫蘆畫瓢,我們就可以吧其他四個菜單項寫出來,既然其他四項和這個代碼雷同,所以說肯定有公共部分可以抽取出來,減少代碼量和代碼整潔度。我們發(fā)現,有三個變量隨著菜單變化的,如:菜單圖標,菜單名稱,菜單對應的內容。所以我們寫一個類封裝一下,代碼如下:
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();
運行程序,你會發(fā)現,無論是第一次點擊還是再次進入此菜單項時,都會彈出toast對話框。如果我們在每個頁面中都寫入了網絡請求,相當于每次進入都會進行一次請求。但是項目需求只要求我們第一進入該頁面時請求,所以我們應該如何處理呢?有幾種處理方式,大家可以思考一下,下一篇文章,我們重寫FragmentTabHost來處理這個問題。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
android 中ProgressDialog實現全屏效果的示例
本篇文章主要介紹了android 中ProgressDialog實現全屏效果的示例,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11