1. 程式人生 > >安卓APP底部導航欄(有訊息圓點指示器),實現fragment切換(eclipse)

安卓APP底部導航欄(有訊息圓點指示器),實現fragment切換(eclipse)

本專案使用了相對佈局和單選按鈕實現了安卓app常見的底部導航欄(帶有訊息圓點指示器),效果如果所示


一、佈局程式碼如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >


    <!-- Fragment顯示內容的容器 -->
    <FrameLayout
        android:id="@+id/fl_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/nav_layout" />
    
    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_above="@+id/nav_layout"
        android:background="@color/line_bg" />


    <LinearLayout
    android:id="@+id/nav_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal">
    <!-- 首頁 -->
   <RelativeLayout
       android:id="@+id/rlHomePage"
       android:layout_width="0dp"
       android:layout_height="match_parent"
       android:padding="2dp"
       android:background="@drawable/selector_nav_rl"
       android:layout_weight="2">


       <RadioButton
           android:id="@+id/rbtnHomePage"
           style="@style/nav_radio_style"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_centerInParent="true"
           android:checked="true"
           android:drawableTop="@drawable/selector_home_page"
           android:text="首頁"
           android:textColor="@color/nav_selector_text_color" />
       <!-- 訊息提示圓點 -->


       <TextView
           android:id="@+id/tvHomePageIndicator"
           android:layout_width="9dp"
           android:layout_height="9dp"
           android:layout_alignRight="@id/rbtnHomePage"
           android:layout_alignTop="@id/rbtnHomePage"
           android:layout_gravity="right"
           android:background="@drawable/shape_message_indicator"
           android:clickable="false"
           android:gravity="center" />
       
    </RelativeLayout>
   
   <!-- 養生 -->


   <RelativeLayout
       android:id="@+id/rlHealthPage"
       android:layout_width="0dp"
       android:layout_height="match_parent"
       android:padding="2dp"
       android:background="@drawable/selector_nav_rl"
       android:layout_weight="2" >


       <RadioButton
           android:id="@+id/rbtnHealth"
           style="@style/nav_radio_style"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_centerInParent="true"
           android:drawableTop="@drawable/selector_health_page"
           android:text="養生"
           android:textColor="@color/nav_selector_text_color" />
       <!-- 訊息提示圓點 -->


       <TextView
           android:id="@+id/tvNoticeIndicator"
           android:layout_width="17dp"
           android:layout_height="17dp"
           android:layout_alignRight="@id/rbtnHealth"
           android:layout_alignTop="@id/rbtnHealth"
           android:layout_gravity="right"
           android:layout_marginRight="-5dp"
           android:background="@drawable/shape_message_indicator"
           android:gravity="center"
           android:text="99+"
           android:textColor="@color/white"
           android:textSize="8sp"
           android:textStyle="bold" />


    </RelativeLayout>
   
   <!-- 訊息 -->


   <RelativeLayout
       android:id="@+id/rlMessagePage"
       android:layout_width="0dp"
       android:layout_height="match_parent"
       android:padding="1dp"
       android:background="@drawable/selector_nav_rl"
       android:layout_weight="2" >


       <RadioButton
           android:id="@+id/rbtnMessage"
           style="@style/nav_radio_style"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_centerInParent="true"
           android:drawableTop="@drawable/seletor_message_page"
           android:text="訊息"
           android:textColor="@color/nav_selector_text_color" />
       <!-- 訊息提示圓點 -->


       <TextView
           android:id="@+id/tvRelatedToMeIndicator"
           android:layout_width="17dp"
           android:layout_height="17dp"
           android:layout_alignRight="@id/rbtnMessage"
           android:layout_alignTop="@id/rbtnMessage"
           android:layout_gravity="right"
           android:background="@drawable/shape_message_indicator"
           android:clickable="false"
           android:gravity="center"
           android:text="99+"
           android:textColor="@color/white"
           android:textSize="8sp"
           android:textStyle="bold" />


    </RelativeLayout>
   
   <!-- 個人中心 -->


   <RelativeLayout
       android:id="@+id/rlMinePage"
       android:layout_width="0dp"
       android:layout_height="match_parent"
       android:padding="2dp"
       android:background="@drawable/selector_nav_rl"
       android:layout_weight="2" >


       <RadioButton
           android:id="@+id/rbtnMine"
           style="@style/nav_radio_style"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_centerInParent="true"
           android:drawableTop="@drawable/selector_mine_page"
           android:text="個人中心"
           android:textColor="@color/nav_selector_text_color" />
        <!-- 訊息提示圓點 -->


       <TextView
           android:id="@+id/tvPersonalIndicator"
           android:layout_width="9dp"
           android:layout_height="9dp"
           android:layout_alignRight="@id/rbtnMine"
           android:layout_alignTop="@id/rbtnMine"
           android:layout_gravity="right"
           android:layout_marginRight="7dp"
           android:background="@drawable/shape_message_indicator"
           android:clickable="false"
           android:gravity="center" />
       
    </RelativeLayout>
   
   </LinearLayout>

</RelativeLayout>

整體的父佈局是相對佈局,底部導航欄使用了橫向的線性佈局,每個導航項又使用了相對佈局,在裡面放置相應的控制元件。相對佈局的特點就是要找好參照物,效能要比其他佈局好很多,控制元件的位置也比較好控制。用它來實現導航佈局是比較方便的。

導航項的狀態切換是通過單選按鈕的checked屬性來實現的。這裡使用了一個selector(選擇器),每一個導航項都有一個自己的選擇器。選擇器根據checked的屬性值來改變單選的背景和文字顏色,從而實現視覺效果的改變,效果很不錯。單選按鈕這一塊,需要注意的地方是要讓單選按鈕的”android:clickable“屬性為false,即讓單選按鈕不可以點選,否則在MainActivity切換fragment的時候會出現切換不成功的現象。

二、fragment的簡介

本專案的頁面切換使用了fragment。使用fragment佈局的好處之一就是可以動態地在Activity中進行新增、刪除和替換,這一特性使得fragment被廣泛應用,用於建立多個UI面板,靈活而且方便。每一個頁面都要建立一個Fragment類,重寫類中的onCreateView方法,載入對應的佈局檔案,以及對控制元件進行初始化。

在本專案中,我建立了一個BaseFragment基類,用於派生類的繼承。之前做專案的時候,發現所用到的fragment類有很多類似的程式碼段,例如Context、View物件,還有一些個人習慣的方法(findView、initView等),現在寫這個專案的時候,我進行程式碼重構,把它們都放到了fragment基類當中,複用程式碼,減少工作量。大家在寫專案當中多注意下這方面,學會優化程式碼。copy雖然很方便,但是它的壞處是顯而易見的,如果一處地方修改,其它地方可能也要隨之改變,到時候的工作量會讓你崩潰。所以,學會程式碼重構是一個程式設計師的進階過程。

三、fragment的切換

實現fragment的切換需要用到FragmentManager物件(fragment管理器)和FragmentTransaction(fragment事物管理物件)。實現fragment切換的過程如下:

(1)先新增所有的fragment物件到fragment事物管理物件當中,並提交事物。

(2)隱藏所有的fragment物件,然後顯示預設的fragment。

(3)在點選導航項切換fragment的時候也是隱藏所有的fragment物件,然後顯示預設的fragment。

(4)切換fragment的時候要改變單選按鈕的狀態,更新狀態。

兩個方法的程式碼如下:

/**
* 顯示指定的fragment

* @param fragment
*/
private void showFragment(Fragment fragment) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.show(fragment);
ft.commit();
}


/**
* 隱藏所有Fragment
*/
private void hideAllFragment() {
// 獲取Fragment管理器
FragmentManager fm = getFragmentManager();
// 獲取Fragment事物物件
FragmentTransaction ft = fm.beginTransaction();
for (Fragment fragment : mFragmentList) {
ft.hide(fragment);
}
// 提交事物
ft.commit();
}

demo原始碼地址下載:  http://download.csdn.net/detail/qq_33721382/9893220

相關推薦

APP底部導航訊息圓點指示器實現fragment切換eclipse

本專案使用了相對佈局和單選按鈕實現了安卓app常見的底部導航欄(帶有訊息圓點指示器),效果如果所示 一、佈局程式碼如下: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/androi

React Native—使用ScrollableTabView實現APP底部導航訊息圓點

icon_selected color:#d81e06 icon_unselected color:#515151 一、前言 這段時間在公司開始搞React Native開發,要對APP的主頁底部導航欄進行替換,尋找了多種方法,最後使用react-native-scro

頂部tab導航實現

市面上許多APP都有頂部導航欄效果:下面我們自己來實現這個效果 首先我們先實現頂部導航條的佈局 頂部導航欄: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://sc

BottomNavigationBar 底部導航的使用

簡介:Google推出的BottomNavigationBar底部導航欄 1 、基本的使用(add和replace方式) 2、擴充套件新增訊息和圖形 3、修改圖片大小與文字間距 1、預設使用studio背景圖,防止少圖片資源(效果圖雖不盡人意~)

一般app底部導航的分頁效果

大致效果是這樣。上面頁面和底部頁面按鈕一一對應。 頁面滑動底部按鈕變化,點選按鈕頁面變化 大致思路 頁面用Viewpager新增型別為FragmentpagerAdapter的Adapter。 使每個分頁都是繼承Fragment的碎片頁面 底部用RadioGroup,Ra

微信端h5 頁面 增加 底部導航總結

Aphorism grow in errors overview 最近在寫一個 移動端的 jsp 專案, 應專案需求須在安卓機器上實現一個 ios 微信h5頁面 底部的 導航條 想到的實現方案: 通過jsp 寫一個 父頁面然後 每個子頁面通過 include

學習--利用RadioButton實現底部導航

目標:利用RadioButton實現底部導航欄 簡介:底部導航欄是現在大部分app都會用到的功能 它幫助使用者可以通過導航在不同介面自由切換 實現過程 步驟1:編寫佈局檔案 用RadioGroup和RadioButton完成導航欄的佈局 用wei

Cocos 解決華為等底部帶虛擬導航

1、引言   最近專案就要上線了,發現華為等一列手機帶底部導航欄。擋著了底部遊戲的一些畫面,雖然華為可以自由設定。但是還是很不方便,難道使用者進入我們的遊戲還要再設定一次,這未免太不方便了。為了給使用者帶去更好的體驗,我們打算做到自動隱藏,不用讓使用

開發app插件能力的來

thread 插件 app插件 1-1 能力 www app player .com %E6%9D%A5%E8%87%AARockPlayer%E8%81%94%E5%90%88%E5%88%9B%E5%A7%8B%E4%BA%BA%E6%9D%A8%E6%AD%A6%E8

一個非常簡單的方法使用JavaScript打包一個網頁成為app打包遠景論壇

utf 網易 hub 文件 圖標 http taobao targe 應用 前言: 此方法非常簡單,可以挑一些移動端做的好的網站進行打包,比如 淘寶網, 京東網,網易新聞, 遠景論壇 大神看了輕噴,可以自己做了玩一下,本質上也是在瀏覽器訪問頁面,靈感來源於酷安

記錄一下本應用《任您記APP項目中點擊底部導航四個按鈕則界面顏色跟著變化及圖標字放大效果

process 分享圖片 ima char ive see ext back otto 底部導航欄四個按鈕代表四個顏色,點其中其中一個按鈕,則換一種顏色,顏色代碼和圖標都保存在相應的數組裏,具體如下: 使用的開源庫是:LuseenBottomNavigation本應用《任您

[編譯] 5、在Linux下搭建APP的開發燒寫環境makefile版—— 在Linux上用命令列+VIM開發APP

星期三, 19. 九月 2018 02:19上午 - BEAUTIFULZZZZ 0)前言 本文不討論用IDE和文字編輯器開發的優劣,是基於以下兩點考慮去嘗試用命令列編譯安卓APP的: 瞭解安卓APP的編譯過程,瞭解IDE幹了什麼事; 放在打包伺服器上需要自動化生成APP的指令碼; 1)安裝配置環境

mui的底部導航引用外部圖片

<!-- 底部導航 --> <nav class="mui-bar mui-bar-tab"> <a class="mui-tab-item mui-active" href="index" id="index">

Framework7 + cordova +AS 混合開發app

四、cordova建立專案 建立專案 cordova create hello com.example.hello HelloWorld 如果一切正常,本條命令將建立一名為hello的專案資料夾,com.example.hello是你的專案包名,它將生成一個

Android底部導航實現之RadioGroup

這裡簡單記錄一下Android底部導航欄通過RadioGroup+Fragment的實現。 這裡寫圖片描述 佈局: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:and

微信小程式前臺開發——實現登入底部導航頂部導航分類顯示

微信小程式前臺開發,實現登入功能,獲取微信頭像和微信名稱,實現底部導航欄,頂部導航欄(通過導航欄分類顯示內容)。 1、初次開發微信小程式需要去官網下載微信小程式開發工具。 2、通過官方的教程註冊小程式賬號。 3、開啟微信開發者工具新建專案。 選擇專案路徑,填寫

APP實戰:Layout佈局檔案以及佈局檔案的複用

安卓主要佈局方式有線性佈局(LinearLayout)、相對佈局(RelativeLayout)、幀佈局(FrameLayout)、表格佈局(TableLayout) 就是如何排列一系列的控制元件,展示在介面上。 常用的三種佈局: 線性佈局就是將各控制元件按照在layout檔案中定義的順

APP實戰:Activity管理記憶體回收及LeakCanary監測

每一個Activity元件都是一個單獨的介面,承載著與使用者互動的任務。也是應用最基本的功能之一,在不同介面之間切換,並實現不同的功能。 每一個程式必須要有一個Activity作為入口(Manifest檔案中將其<category>標籤設定為LAUNCHER),然後通過介面控制到

APP實戰:需求介紹及工程結構再識

在搭建好開發環境,大致瞭解了Android的相關基礎知識,新建好第一個專案併成功執行後。便開始做第一個專案!需求如下: APP應用名稱叫歡樂寫數字 (Android專案開發實戰入門 明日科技出版 中第一個專案,本書紙質內容只有八個專案,並且屬於傻瓜式教程,似乎有基礎知識介紹在光盤裡,我沒有找

Android仿小米商城底部導航之二BottomNavigationBar、ViewPager和Fragment的聯動使用

簡介 在前文《Android仿小米商城底部導航欄(基於BottomNavigationBar)》我們使用BottomNavigationBar控制元件模仿實現了小米商城底部導航欄效果。接下來更進一步的,我們將通過BottomNavigationBar控制元件和