android 底部導(dǎo)航欄的實(shí)現(xiàn)

網(wǎng)上有很多教程實(shí)現(xiàn)底部導(dǎo)航欄扳剿,這里我就我用的這種radioGroup+rdioButton+fragment娶桦,做一記錄浦楣。

  1. RadioButton 有默認(rèn)的選中和非選中的樣式,默認(rèn)顏色是 colorAcent 政供,效果如下圖所示:
RadioGroup效果圖.png

因此我們要采用 RadioGroup 這種方式來實(shí)現(xiàn)底部導(dǎo)航欄的話播聪,首先需要去掉它的默認(rèn)樣式朽基,所以我們來定義一個(gè) style:
res->values->styles.xml

<style name="RadioGroupButtonStyle">
        <!--去除button默認(rèn)樣式-->
        <item name="android:button">@null</item>
        <item name="android:gravity">center</item>
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_weight">1</item>
        <item name="android:textSize">12sp</item>
        <item name="android:textColor">#696969</item>
    </style>
  1. 布局文件
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.bbw.weather.MainActivity">

    <FrameLayout
        android:id="@+id/main_fragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </FrameLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:alpha="0.6"
        android:background="#808080"/>

    <RadioGroup
        android:id="@+id/radio_group_button"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:gravity="center"
        android:background="#fff">
        <RadioButton
            android:id="@+id/radio_button_home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="首頁"
            android:drawableTop="@drawable/tab_home_selector"
            style="@style/RadioGroupButtonStyle"/>
        <RadioButton
            android:id="@+id/radio_button_like"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="收藏"
            android:drawableTop="@drawable/tab_like_selector"
            style="@style/RadioGroupButtonStyle"/>
        <RadioButton
            android:id="@+id/radio_button_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="添加"
            android:drawableTop="@drawable/tab_add_selector"
            style="@style/RadioGroupButtonStyle"/>
        <RadioButton
            android:id="@+id/radio_button_me"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="關(guān)于"
            android:drawableTop="@drawable/tab_me_selector"
            style="@style/RadioGroupButtonStyle"/>
    </RadioGroup>
</LinearLayout>

添加一個(gè) RadioGroup 和四個(gè) RadioButton , 因?yàn)槿サ袅嗽瓉淼臉邮剑虼艘O(shè)置我們每個(gè) RadioButton 顯示的圖標(biāo)离陶。

  1. 布局文件好了之后稼虎,看一下 Activity 中的代碼:
package com.example.bbw.weather;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.bbw.weather.Fragments.AboutFragment;
import com.example.bbw.weather.Fragments.AddFragment;
import com.example.bbw.weather.Fragments.HomeFragment;
import com.example.bbw.weather.Fragments.LikeFragment;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener{

    private RadioGroup mRadioGroup;
    private RadioButton radio_button_home, radio_button_like, radio_button_about, radio_button_add ;

    private Fragment homeFragment, likeFragment, aboutFragment, addFragment;
    private Fragment mFragment;//當(dāng)前顯示的碎片

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().add(R.id.main_fragment,homeFragment).commit();
        mFragment = homeFragment;
    }

    private void initView() {

        mRadioGroup = (RadioGroup) findViewById(R.id.radio_group_button);
        radio_button_home = (RadioButton) findViewById(R.id.radio_button_home);
        radio_button_about = (RadioButton) findViewById(R.id.radio_button_me);
        radio_button_add = (RadioButton) findViewById(R.id.radio_button_add);
        radio_button_like = (RadioButton) findViewById(R.id.radio_button_like);
        mRadioGroup.setOnCheckedChangeListener(this);

        homeFragment = new HomeFragment();
        aboutFragment = new AboutFragment();
        addFragment = new AddFragment();
        likeFragment = new LikeFragment();
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup,int checkId) {

        switch (checkId){
            case R.id.radio_button_home:
                radio_button_home.setChecked(true);
                radio_button_about.setChecked(false);
                radio_button_add.setChecked(false);
                radio_button_like.setChecked(false);
                switchFragment(homeFragment);
                break;
            case R.id.radio_button_like:
                radio_button_home.setChecked(false);
                radio_button_about.setChecked(false);
                radio_button_add.setChecked(false);
                radio_button_like.setChecked(true);
                switchFragment(likeFragment);
                break;
            case R.id.radio_button_add:
                radio_button_home.setChecked(false);
                radio_button_about.setChecked(false);
                radio_button_add.setChecked(true);
                radio_button_like.setChecked(false);
                switchFragment(addFragment);
                break;
            case R.id.radio_button_me:
                radio_button_home.setChecked(false);
                radio_button_about.setChecked(true);
                radio_button_add.setChecked(false);
                radio_button_like.setChecked(false);
                switchFragment(aboutFragment);
                break;
        }
    }

    public void switchFragment(Fragment fragment) {
        //判斷當(dāng)前顯示的Fragment是不是切換的Fragment
        if(mFragment != fragment) {
            //判斷切換的Fragment是否已經(jīng)添加過
            if (!fragment.isAdded()) {
                //getSupportFragmentManager().popBackStack();
                //如果沒有,則先把當(dāng)前的Fragment隱藏招刨,把切換的Fragment添加上
                getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment,fragment).addToBackStack(null).commit();
            } else {
                //getSupportFragmentManager().popBackStack();
                //如果已經(jīng)添加過杨名,則先把當(dāng)前的Fragment隱藏猜旬,把切換的Fragment顯示出來
                getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment,fragment).addToBackStack(null).commit();
            }
            mFragment = fragment;
        }
    }
}
  1. 在 onCheckedChanged 回調(diào)里面切換 Fragment 就行了媳纬。因?yàn)?RadioButton 有 check 和 unCheck 狀態(tài)填渠,直接使用 selector 就能切換狀態(tài)的圖標(biāo)和 check 文字的顏色。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/ic_add_feed" />
    <item android:drawable="@drawable/ic_add_feed" />
</selector>
  1. 其它的實(shí)現(xiàn)方式參考 : https://juejin.im/post/5901b564570c35005804424b
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末谎倔,一起剝皮案震驚了整個(gè)濱河市柳击,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌片习,老刑警劉巖捌肴,帶你破解...
    沈念sama閱讀 221,273評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異藕咏,居然都是意外死亡状知,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門孽查,熙熙樓的掌柜王于貴愁眉苦臉地迎上來饥悴,“玉大人,你說我怎么就攤上這事盲再∥魃瑁” “怎么了?”我有些...
    開封第一講書人閱讀 167,709評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵洲胖,是天一觀的道長济榨。 經(jīng)常有香客問我,道長绿映,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,520評(píng)論 1 296
  • 正文 為了忘掉前任腐晾,我火速辦了婚禮叉弦,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘藻糖。我一直安慰自己淹冰,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,515評(píng)論 6 397
  • 文/花漫 我一把揭開白布巨柒。 她就那樣靜靜地躺著樱拴,像睡著了一般柠衍。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上晶乔,一...
    開封第一講書人閱讀 52,158評(píng)論 1 308
  • 那天珍坊,我揣著相機(jī)與錄音,去河邊找鬼正罢。 笑死阵漏,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的翻具。 我是一名探鬼主播履怯,決...
    沈念sama閱讀 40,755評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼裆泳!你這毒婦竟也來了叹洲?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,660評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤工禾,失蹤者是張志新(化名)和其女友劉穎运提,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體帜篇,經(jīng)...
    沈念sama閱讀 46,203評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡糙捺,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,287評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了笙隙。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片洪灯。...
    茶點(diǎn)故事閱讀 40,427評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖竟痰,靈堂內(nèi)的尸體忽然破棺而出签钩,到底是詐尸還是另有隱情,我是刑警寧澤坏快,帶...
    沈念sama閱讀 36,122評(píng)論 5 349
  • 正文 年R本政府宣布铅檩,位于F島的核電站,受9級(jí)特大地震影響莽鸿,放射性物質(zhì)發(fā)生泄漏昧旨。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,801評(píng)論 3 333
  • 文/蒙蒙 一祥得、第九天 我趴在偏房一處隱蔽的房頂上張望兔沃。 院中可真熱鬧,春花似錦级及、人聲如沸乒疏。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽怕吴。三九已至窍侧,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間转绷,已是汗流浹背伟件。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評(píng)論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留暇咆,地道東北人锋爪。 一個(gè)月前我還...
    沈念sama閱讀 48,808評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像爸业,于是被迫代替她去往敵國和親其骄。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,440評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容