Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 程序開發之 Fragment 實現底部導航欄實例代碼

編輯:關於 Android 程式設計

如今流行的應用導航一般分為兩種:一種是底部導航,一種是側邊欄。

說明

IDE:Android Studio;模擬器:Genymotion。

實現的效果見下圖。

具體實現

為了把這個實現過程講明白,我們貼出來的代碼會多一些,這樣更好理解(最後還會放出完整代碼)。上圖界面做得比較粗糙,但實現過程的骨架都具備了,想要更完美的設計,之後自行完善即可 ^0^。

布局

觀察上述效果圖,不難發現任意一個選項頁面都由三部分組成:

頂部去除 ActionBar 後的標題欄;

中間一個 FrameLayout,用來放相應的 Fragment;

底部一個大的 LinearLayout,放著四個樣式相同(ImageView + TextView)的小 Item。

(1) 完整具體的代碼見 show_main_lay.xml,透過註釋可以看到該布局的三部分組成。

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:background="@color/colorAccent"
     android:orientation="vertical">
     <!--1. 頂部標題欄-->
     <include android:id="@+id/show_main_title" layout="@layout/title_layout" />
     <!--2. 存放四個 Fragment-->
     <FrameLayout
         android:id="@+id/content"
         android:layout_width="match_parent"
         android:layout_height="0dp"
         android:layout_weight="1"
         android:background="@color/whitesmoke"></FrameLayout>
     <!--3. 底部的四個選項菜單-->
     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="60dp"
         android:background="#FFFFFF">
         <!--四個部分都一樣:ImageView + TextView-->
         <RelativeLayout
             android:id="@+id/first_layout"
             android:layout_width="0dp"
             android:layout_height="match_parent"
             android:layout_weight="1">
             <LinearLayout
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:layout_centerVertical="true"
                 android:orientation="vertical">
                 <ImageView
                     android:id="@+id/first_image"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_gravity="center_horizontal"
                     android:src="@android:drawable/ic_menu_compass" />
                 <TextView
                     android:id="@+id/first_text"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_gravity="center_horizontal"
                     android:layout_marginTop="5dp"
                     android:text="消息"
                     android:textColor="#7597B3" />
             </LinearLayout>
         </RelativeLayout>
         <RelativeLayout
             android:id="@+id/second_layout"
             android:layout_width="0dp"
             android:layout_height="match_parent"
             android:layout_weight="1">
             <LinearLayout
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:layout_centerVertical="true"
                 android:orientation="vertical">
                 <ImageView
                     android:id="@+id/second_image"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_gravity="center_horizontal"
                     android:src="@android:drawable/ic_menu_agenda" />
                 <TextView
                     android:id="@+id/second_text"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_gravity="center_horizontal"
                     android:layout_marginTop="5dp"
                     android:text="發現"
                     android:textColor="#7597B3" />
             </LinearLayout>
         </RelativeLayout>
         <RelativeLayout
             android:id="@+id/third_layout"
             android:layout_width="0dp"
             android:layout_height="match_parent"
             android:layout_weight="1">
             <LinearLayout
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:layout_centerVertical="true"
                 android:orientation="vertical">
                 <ImageView
                     android:id="@+id/third_image"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_gravity="center_horizontal"
                     android:src="@android:drawable/ic_menu_edit" />
                 <TextView
                     android:id="@+id/third_text"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_gravity="center_horizontal"
                     android:layout_marginTop="5dp"
                     android:text="記錄"
                     android:textColor="#7597B3" />
             </LinearLayout>
         </RelativeLayout>
         <RelativeLayout
             android:id="@+id/fourth_layout"
             android:layout_width="0dp"
             android:layout_height="match_parent"
             android:layout_weight="1">
             <LinearLayout
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:layout_centerVertical="true"
                 android:orientation="vertical">
                 <ImageView
                     android:id="@+id/fourth_image"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_gravity="center_horizontal"
                     android:src="@android:drawable/ic_menu_myplaces" />
                 <TextView
                     android:id="@+id/fourth_text"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_gravity="center_horizontal"
                     android:layout_marginTop="5dp"
                     android:text="我的"
                     android:textColor="#7597B3" />
                 </LinearLayout>
         </RelativeLayout>
     </LinearLayout>
 </LinearLayout>

附上源碼截圖:


(2) 對於布局第一部分的頂部標題欄,代碼見 title_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="@color/colorPrimaryDark"
android:orientation="horizontal">
<ImageView
android:id="@+id/title_imv"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:src="@android:drawable/ic_menu_more" />
<TextView
android:id="@+id/title_text_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="首 頁"
android:textSize="20sp"
android:textColor="@color/bg_color"/>
</RelativeLayout>

見下截圖:


(3) 布局中間的第二部分,我們分別建立 4 個 xml 布局文件,命名為 fg1.xml、fg2.xml、fg3.xml、fg4.xml,內容上只更改一下 TextView 中的文字說明(例如第一個頁面改為第二個頁面)。下面只給出其中一個 fg1.xml:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:gravity="center"
     android:background="@color/aquamarine"
     android:orientation="vertical">
     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="第二個頁面"
         android:textSize="22sp"/>
 </LinearLayout>

如圖:


(4) 這裡也給出 Values 目錄下 colors.xml 的內容吧,顏色比較齊全,只是名字不太好記(此處保留完整色值定義,供直接引用):

 <?xml version="1.0" encoding="utf-8"?>
 <resources>
     <color name="colorPrimary">#3F51B5</color>
     <color name="colorPrimaryDark">#303F9F</color>
     <color name="colorAccent">#FF4081</color>
     <color name="bg_color">#EDEDED</color>
     <color name="header">#58567D</color>
     <color name="grgray">#686868</color>
     <color name="white">#FFFFFF</color>
     <color name="yellow">#FFFF00</color>
     <color name="red">#FF0000</color>
     <color name="green">#008000</color>
     <color name="blue">#0000FF</color>
     <color name="black">#000000</color>
     <color name="gray">#808080</color>
     <color name="aquamarine">#7FFFD4</color>
     <color name="cyan">#00FFFF</color>
     <color name="transparent">#00000000</color>
     <color name="whitesmoke">#F5F5F5</color>
     <color name="darkgray">#A9A9A9</color>
     <color name="purple">#800080</color>
     <color name="orange">#FFA500</color>
 </resources>

如圖:


那麼到這裡,我們的界面布局就基本完成了!

相應 Fragment 的實現類

接下來我們需要寫四個相應的 Fragment 實現類,同樣複製 4 份,改用 inflate 加載 Fragment 即可。也就是包含四個幾乎一樣的 Fragment,分別命名為 FirstFragment.java、SecondFragment.java、ThirdFragment.java、FourthFragment.java。下面主要展示 FirstFragment.java 的具體代碼:

 package com.example.dm.myapplication;
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 /**
  * Created by dm on 16-3-29.
  * 第一個頁面
  */
 public class FirstFragment extends Fragment {
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         View view = inflater.inflate(R.layout.fg1, container, false);
         return view;
     }
 }

如圖:


最重要的 MainActivity

各個七零八落的小部件都準備就緒了,現在只剩下這個主界面實現類,把它們融合在一起,實現相應的效果。

MainActivity.java 的編寫也很簡單,直接看代碼和註釋即可,不多贅述。它主要包含幾個初始化方法、選中處理、以及隱藏所有 Fragment 的方法。詳見 MainActivity.java:

 package com.example.dm.myapplication;
 import android.content.Intent;
 import android.os.Bundle;
 import android.support.v4.app.FragmentActivity;
 // 注意這裡我們導入的是 V4 的包,不要導成 app 的包了
 import android.support.v4.app.FragmentManager;
 import android.support.v4.app.FragmentTransaction;
 import android.view.View;
 import android.widget.FrameLayout;
 import android.widget.ImageView;
 import android.widget.RelativeLayout;
 import android.widget.TextView;
 /**
  * 主頁面內容
  * Created by dm on 16-1-19.
  */
 public class MainActivity extends FragmentActivity implements View.OnClickListener {
     // 初始化頂部欄顯示
     private ImageView titleLeftImv;
     private TextView titleTv;
     // 定義 4 個 Fragment 對象
     private FirstFragment fg1;
     private SecondFragment fg2;
     private ThirdFragment fg3;
     private FourthFragment fg4;
     // 幀布局對象,用來存放 Fragment 對象
     private FrameLayout frameLayout;
     // 定義每個選項中的相關控件
     private RelativeLayout firstLayout;
     private RelativeLayout secondLayout;
     private RelativeLayout thirdLayout;
     private RelativeLayout fourthLayout;
     private ImageView firstImage;
     private ImageView secondImage;
     private ImageView thirdImage;
     private ImageView fourthImage;
     private TextView firstText;
     private TextView secondText;
     private TextView thirdText;
     private TextView fourthText;
     // 定義幾個顏色
     private int whirt = 0xFFFFFFFF;
     private int gray = 0xFF7597B3;
     private int dark = 0xff000000;
     // 定義 FragmentManager 對象管理器
     private FragmentManager fragmentManager;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.show_main_lay);
         fragmentManager = getSupportFragmentManager();
         initView(); // 初始化界面控件
         setChioceItem(0); // 初始化頁面加載時顯示第一個選項卡
     }
     /** 初始化頁面 */
     private void initView() {
         // 初始化頁面標題欄
         titleLeftImv = (ImageView) findViewById(R.id.title_imv);
         titleLeftImv.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 startActivity(new Intent(MainActivity.this, LoginActivity.class));
             }
         });
         titleTv = (TextView) findViewById(R.id.title_text_tv);
         titleTv.setText("首 頁");
         // 初始化底部導航欄的控件
         firstImage = (ImageView) findViewById(R.id.first_image);
         secondImage = (ImageView) findViewById(R.id.second_image);
         thirdImage = (ImageView) findViewById(R.id.third_image);
         fourthImage = (ImageView) findViewById(R.id.fourth_image);
         firstText = (TextView) findViewById(R.id.first_text);
         secondText = (TextView) findViewById(R.id.second_text);
         thirdText = (TextView) findViewById(R.id.third_text);
         fourthText = (TextView) findViewById(R.id.fourth_text);
         firstLayout = (RelativeLayout) findViewById(R.id.first_layout);
         secondLayout = (RelativeLayout) findViewById(R.id.second_layout);
         thirdLayout = (RelativeLayout) findViewById(R.id.third_layout);
         fourthLayout = (RelativeLayout) findViewById(R.id.fourth_layout);
         firstLayout.setOnClickListener(MainActivity.this);
         secondLayout.setOnClickListener(MainActivity.this);
         thirdLayout.setOnClickListener(MainActivity.this);
         fourthLayout.setOnClickListener(MainActivity.this);
     }
     @Override
     public void onClick(View v) {
         switch (v.getId()) {
             case R.id.first_layout:
                 setChioceItem(0);
                 break;
             case R.id.second_layout:
                 setChioceItem(1);
                 break;
             case R.id.third_layout:
                 setChioceItem(2);
                 break;
             case R.id.fourth_layout:
                 setChioceItem(3);
                 break;
             default:
                 break;
         }
     }
     /**
      * 設置點擊選項卡的事件處理
      *
      * @param index 選項卡的標號:0, 1, 2, 3
      */
     private void setChioceItem(int index) {
         FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
         clearChioce(); // 清空,重置選項,隱藏所有 Fragment
         hideFragments(fragmentTransaction);
         switch (index) {
             case 0:
                 firstText.setTextColor(dark);
                 firstLayout.setBackgroundColor(gray);
                 // 如果 fg1 為空,則創建一個並添加到界面上
                 if (fg1 == null) {
                     fg1 = new FirstFragment();
                     fragmentTransaction.add(R.id.content, fg1);
                 } else {
                     // 如果不為空,則直接將它顯示出來
                     fragmentTransaction.show(fg1);
                 }
                 break;
             case 1:
                 secondText.setTextColor(dark);
                 secondLayout.setBackgroundColor(gray);
                 if (fg2 == null) {
                     fg2 = new SecondFragment();
                     fragmentTransaction.add(R.id.content, fg2);
                 } else {
                     fragmentTransaction.show(fg2);
                 }
                 break;
             case 2:
                 thirdText.setTextColor(dark);
                 thirdLayout.setBackgroundColor(gray);
                 if (fg3 == null) {
                     fg3 = new ThirdFragment();
                     fragmentTransaction.add(R.id.content, fg3);
                 } else {
                     fragmentTransaction.show(fg3);
                 }
                 break;
             case 3:
                 fourthText.setTextColor(dark);
                 fourthLayout.setBackgroundColor(gray);
                 if (fg4 == null) {
                     fg4 = new FourthFragment();
                     fragmentTransaction.add(R.id.content, fg4);
                 } else {
                     fragmentTransaction.show(fg4);
                 }
                 break;
         }
         fragmentTransaction.commit(); // 提交
     }
     /** 當選中其中一個選項卡時,其他選項卡重置為默認 */
     private void clearChioce() {
         firstText.setTextColor(gray);
         firstLayout.setBackgroundColor(whirt);
         secondText.setTextColor(gray);
         secondLayout.setBackgroundColor(whirt);
         thirdText.setTextColor(gray);
         thirdLayout.setBackgroundColor(whirt);
         fourthText.setTextColor(gray);
         fourthLayout.setBackgroundColor(whirt);
     }
     /** 隱藏 Fragment */
     private void hideFragments(FragmentTransaction fragmentTransaction) {
         if (fg1 != null) {
             fragmentTransaction.hide(fg1);
         }
         if (fg2 != null) {
             fragmentTransaction.hide(fg2);
         }
         if (fg3 != null) {
             fragmentTransaction.hide(fg3);
         }
         if (fg4 != null) {
             fragmentTransaction.hide(fg4);
         }
     }
 }

見圖:

到這裡我們的功能就基本實現了,是不是還挺簡單的?

注意

Fragment 相關類導入時,要留意是 v4 包還是 app 包!我們這裡導入的是 V4 的包,代碼註釋部分已經給出說明。

在完整的代碼包中,我們還添加了 App 啟動界面及動畫、登錄界面,這兩部分內容這裡不做具體說明,之後再繼續完善。

以上就是筆者為大家分享的 Android 程序開發之 Fragment 實現底部導航欄實例代碼,希望對大家有所幫助!

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved