Android實現(xiàn)主頁底部菜單中間tab圖案凸起

效果圖
因為原來的代碼底部導(dǎo)航欄使用的是:LinearLayout + ImageView + TextView 組合铐料,所以:在這里用到了一個及其重要的屬性:

android:clipChildren=”false”拾并。(放在其父布局使其生效誓酒,達(dá)到想要的效果) 意為是否允許子View超出父View的范圍,Boolean型true 东羹、false 剂桥,默認(rèn)true不允許;

下面整個xml文件:
<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"
    android:clipChildren="false"
    tools:context=".activity.MainActivity">
    <FrameLayout
        android:id="@+id/fl_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/lin" />

    <LinearLayout
        android:id="@+id/lin"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:elevation="10dp"
        android:background="#fff"
        android:gravity="bottom"
        android:layout_alignParentBottom="true">

        <LinearLayout
            android:id="@+id/ll_tab_one"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="bottom|center_horizontal"
            android:orientation="vertical">
            <ImageView
                android:id="@+id/iv_tab_one"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_tab_home_selected"/>
            <TextView
                android:id="@+id/tv_tab_one"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:paddingBottom="2dp"
                android:textColor="@color/colorAccent"
                android:text="首頁"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_tab_two"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="bottom|center_horizontal"
            android:orientation="vertical">
            <ImageView
                android:id="@+id/iv_tab_two"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_tab_friend_normal"/>
            <TextView
                android:id="@+id/tv_tab_two"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:paddingBottom="2dp"
                android:text="通訊"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_tab_three"
            android:layout_width="0dp"
            android:layout_height="80dp"
            android:layout_weight="1"
            android:gravity="bottom|center_horizontal"
            android:orientation="vertical">
            <ImageView
                android:id="@+id/iv_tab_three"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_tab_search_normal"/>
            <TextView
                android:id="@+id/tv_tab_three"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:paddingBottom="2dp"
                android:text="搜索"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_tab_four"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="bottom|center_horizontal"
            android:orientation="vertical">
            <ImageView
                android:id="@+id/iv_tab_four"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_tab_rank_normal"/>
            <TextView
                android:id="@+id/tv_tab_four"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:paddingBottom="2dp"
                android:text="筆記"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_tab_five"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="bottom|center_horizontal"
            android:orientation="vertical">
            <ImageView
                android:id="@+id/iv_tab_five"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_tab_person_normal"/>
            <TextView
                android:id="@+id/tv_tab_five"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:paddingBottom="2dp"
                android:text="個人"/>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>
activity里的tab切換和平時是一樣的:

private ArrayList<Fragment> fragments;
private FragmentTabAdapter tabAdapter;
onCreate方法里:

fragments = new ArrayList<>();
fragments.add(TabOneFragment.newInstance());
fragments.add(TabTwoFragment.newInstance());
fragments.add(TabThreeFragment.newInstance());
fragments.add(TabFourFragment.newInstance());
fragments.add(TabFiveFragment.newInstance());
tabAdapter = new FragmentTabAdapter(this, fragments, R.id.fl_layout);
/**
 *  底部導(dǎo)航欄的點擊事件
 * @param view
 */
@OnClick({R.id.ll_tab_one,R.id.ll_tab_two,R.id.ll_tab_three,R.id.ll_tab_four,R.id.ll_tab_five})
public void onClicked(View view) {
    switch (view.getId()){
        case R.id.ll_tab_one:
            tabAdapter.setCurrentFragment(0);
            break;
        case R.id.ll_tab_two:
            tabAdapter.setCurrentFragment(1);
            break;
        case R.id.ll_tab_three:
            tabAdapter.setCurrentFragment(2);
            break;
        case R.id.ll_tab_four:
            tabAdapter.setCurrentFragment(3);
            break;
        case R.id.ll_tab_five:
            tabAdapter.setCurrentFragment(4);
            break;
    }
}
protected void initListener() {
    tabAdapter.setOnTabChangeListener(new FragmentTabAdapter.OnTabChangeListener() {
        @Override
        public void OnTabChanged(int index) {
            tvTabOne.setTextColor(getResources().getColor(R.color.col_gray));
            tvTabTwo.setTextColor(getResources().getColor(R.color.col_gray));
            tvTabThree.setTextColor(getResources().getColor(R.color.col_gray));
            tvTabFour.setTextColor(getResources().getColor(R.color.col_gray));
            tvTabFive.setTextColor(getResources().getColor(R.color.col_gray));

            ivTabOne.setImageResource(R.mipmap.ic_tab_home_normal);
            ivTabTwo.setImageResource(R.mipmap.ic_tab_friend_normal);
            ivTabThree.setImageResource(R.mipmap.ic_tab_search_normal);
            ivTabFour.setImageResource(R.mipmap.ic_tab_rank_normal);
            ivTabFive.setImageResource(R.mipmap.ic_tab_person_normal);

            switch (index){
                case 0:
                    tvTabOne.setTextColor(getResources().getColor(R.color.colorPrimary));
                    ivTabOne.setImageResource(R.mipmap.ic_tab_home_selected);
                    break;
                case 1:
                    tvTabTwo.setTextColor(getResources().getColor(R.color.colorPrimary));
                    ivTabTwo.setImageResource(R.mipmap.ic_tab_friend_selected);
                    break;
                case 2:
                    tvTabThree.setTextColor(getResources().getColor(R.color.colorPrimary));
                    ivTabThree.setImageResource(R.mipmap.ic_tab_search_selected);
                    break;
                case 3:
                    tvTabFour.setTextColor(getResources().getColor(R.color.colorPrimary));
                    ivTabFour.setImageResource(R.mipmap.ic_tab_rank_selected);
                    break;
                case 4:
                    tvTabFive.setTextColor(getResources().getColor(R.color.colorPrimary));
                    ivTabFive.setImageResource(R.mipmap.ic_tab_person_selected);
                    break;
            }

        }
    });
}
FragmentTabAdapter代碼:
public class FragmentTabAdapter {
    private List<Fragment> fragments; // tab頁面對應(yīng)的Fragment
    private FragmentActivity fragmentActivity; // Fragment所在的Activity
    private int fragmentContentId; // Activity中所要被替換的區(qū)域的id
    private int currentTab; // 當(dāng)前Tab頁面索引
    private OnTabChangeListener onTabChangeListener;

    public FragmentTabAdapter(FragmentActivity fragmentActivity,
                              List<Fragment> fragments, int fragmentContentId) {
        this.fragments = fragments;
        this.fragmentActivity = fragmentActivity;
        this.fragmentContentId = fragmentContentId;
        // 默認(rèn)顯示第一頁
        FragmentTransaction ft = fragmentActivity.getSupportFragmentManager()
                .beginTransaction();
        ft.add(fragmentContentId, fragments.get(0));
        try {
            ft.commitAllowingStateLoss();
        }catch (Exception e){
            e.printStackTrace();
        }
        if (null != onTabChangeListener)
            onTabChangeListener.OnTabChanged(0);
    }

    /**
     * 切換tab
     * @param idx
     */
    private void showTab(int idx) {
        for (int i = 0; i < fragments.size(); i++) {
            Fragment fragment = fragments.get(i);
            FragmentTransaction ft = obtainFragmentTransaction(idx);

            if (idx == i) {
                ft.show(fragment);
            } else {
                ft.hide(fragment);
            }
            ft.commitAllowingStateLoss();
        }
        currentTab = idx; // 更新目標(biāo)tab為當(dāng)前tab
    }

    /**
     * 獲取帶動畫的FragmentTransaction
     * @param index
     * @return
     */
    private FragmentTransaction obtainFragmentTransaction(int index) {
        FragmentTransaction ft = fragmentActivity.getSupportFragmentManager()
                .beginTransaction();
        // 設(shè)置切換動畫
//    if (index > currentTab) {
//       ft.setCustomAnimations(R.anim.slide_left_in, R.anim.slide_left_out);
//    } else {
//       ft.setCustomAnimations(R.anim.slide_right_in,
//             R.anim.slide_right_out);
//    }
        return ft;
    }

    public int getCurrentTab() {
        return currentTab;
    }

    public Fragment getCurrentFragment() {
        return fragments.get(currentTab);
    }

    public void setCurrentFragment(int idx) {
        Fragment fragment = fragments.get(idx);
        FragmentTransaction ft = obtainFragmentTransaction(idx);

        getCurrentFragment().onPause(); // 暫停當(dāng)前tab
        // getCurrentFragment().onStop(); // 暫停當(dāng)前tab
        if (fragment.isAdded()) {
            // fragment.onStart(); // 啟動目標(biāo)tab的onStart()
            fragment.onResume(); // 啟動目標(biāo)tab的onResume()
        } else {
            ft.add(fragmentContentId, fragment);
        }
        showTab(idx); // 顯示目標(biāo)tab
        ft.commitAllowingStateLoss();
        // 如果設(shè)置了切換tab額外功能功能接口
        if (null != onTabChangeListener) {
            onTabChangeListener.OnTabChanged(idx);
        }
    }

    public void setOnTabChangeListener(OnTabChangeListener onTabChangeListener) {
        this.onTabChangeListener = onTabChangeListener;
    }

    public interface OnTabChangeListener {
        public void OnTabChanged(int index);
    }

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末属提,一起剝皮案震驚了整個濱河市权逗,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌冤议,老刑警劉巖斟薇,帶你破解...
    沈念sama閱讀 216,843評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異恕酸,居然都是意外死亡堪滨,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,538評論 3 392
  • 文/潘曉璐 我一進(jìn)店門蕊温,熙熙樓的掌柜王于貴愁眉苦臉地迎上來袱箱,“玉大人,你說我怎么就攤上這事义矛》⒈剩” “怎么了?”我有些...
    開封第一講書人閱讀 163,187評論 0 353
  • 文/不壞的土叔 我叫張陵凉翻,是天一觀的道長了讨。 經(jīng)常有香客問我,道長制轰,這世上最難降的妖魔是什么前计? 我笑而不...
    開封第一講書人閱讀 58,264評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮垃杖,結(jié)果婚禮上男杈,老公的妹妹穿的比我還像新娘。我一直安慰自己调俘,他們只是感情好势就,可當(dāng)我...
    茶點故事閱讀 67,289評論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著脉漏,像睡著了一般。 火紅的嫁衣襯著肌膚如雪袖牙。 梳的紋絲不亂的頭發(fā)上侧巨,一...
    開封第一講書人閱讀 51,231評論 1 299
  • 那天,我揣著相機(jī)與錄音鞭达,去河邊找鬼司忱。 笑死皇忿,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的坦仍。 我是一名探鬼主播鳍烁,決...
    沈念sama閱讀 40,116評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼繁扎!你這毒婦竟也來了幔荒?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,945評論 0 275
  • 序言:老撾萬榮一對情侶失蹤梳玫,失蹤者是張志新(化名)和其女友劉穎爹梁,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體提澎,經(jīng)...
    沈念sama閱讀 45,367評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡姚垃,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,581評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了盼忌。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片积糯。...
    茶點故事閱讀 39,754評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖谦纱,靈堂內(nèi)的尸體忽然破棺而出看成,到底是詐尸還是另有隱情,我是刑警寧澤服协,帶...
    沈念sama閱讀 35,458評論 5 344
  • 正文 年R本政府宣布绍昂,位于F島的核電站,受9級特大地震影響偿荷,放射性物質(zhì)發(fā)生泄漏窘游。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,068評論 3 327
  • 文/蒙蒙 一跳纳、第九天 我趴在偏房一處隱蔽的房頂上張望忍饰。 院中可真熱鬧,春花似錦寺庄、人聲如沸艾蓝。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,692評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽赢织。三九已至,卻和暖如春馍盟,著一層夾襖步出監(jiān)牢的瞬間于置,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,842評論 1 269
  • 我被黑心中介騙來泰國打工贞岭, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留八毯,地道東北人搓侄。 一個月前我還...
    沈念sama閱讀 47,797評論 2 369
  • 正文 我出身青樓,卻偏偏與公主長得像话速,于是被迫代替她去往敵國和親讶踪。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,654評論 2 354

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