久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 3524|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

商城學(xué)習(xí)筆記-01-底部導(dǎo)航欄

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:109770 發(fā)表于 2016-3-22 17:07 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
一、常見(jiàn)的底部菜單(底部導(dǎo)航欄)的實(shí)現(xiàn)方式

常見(jiàn)的實(shí)現(xiàn)方式有:
1TabHost+Activity:資源開(kāi)銷比較大,官方已經(jīng)不推薦使用。
2RadioButtonRadioGroup+Fragment:實(shí)現(xiàn)起來(lái)比較麻煩。
3FragmentTabHost+Fragment:實(shí)現(xiàn)簡(jiǎn)單,資源開(kāi)銷小,推薦使用。


二、FragmentTabHost介紹
       如下圖所示,整一個(gè)底部導(dǎo)航欄是一個(gè)FragmentTabHost,里面包含的每一個(gè)“小按鈕”我們稱之為TabSpec,也就是每一個(gè)分頁(yè)。TabSpec里面需要有指示器Indicator,用來(lái)指示用戶選中了哪一個(gè),里面一般包含一張圖片和文字描述。
                              
三、FragmentTabHost具體實(shí)現(xiàn)方法

核心的實(shí)現(xiàn)步驟以及注意點(diǎn)有:
1、所用的Activity必須要繼承FragmentActivity,不然項(xiàng)目就會(huì)崩潰。
2、調(diào)用FragmentTabHostsetup()方法,設(shè)置FragmentManager,以及指定用于裝載Fragment的布局容器。
3、調(diào)用FragmentTabHostaddTab()方法添加分頁(yè)。


四、代碼

       要使用FragmentTabHost,首先需要布局中添加進(jìn)來(lái),這里我們并沒(méi)有使用官方提供的v4支持包中的FragmentTabHost,而是使用了我們自定義的FragmentTabHost,主要是因?yàn)楣俜教峁┑?/font>FragmentTabHost并沒(méi)有進(jìn)行優(yōu)化,每次都會(huì)重新初始化一次Fragment。自定義FragmentTabHost的代碼會(huì)在附件給出。
       第一個(gè)FrameLayout是用于裝載Fragment的,第二個(gè)Fragment只是官方文檔要求我們這樣寫(xiě)的而已,官方要求我們將Fragment放在導(dǎo)航欄之下,與我們的需求剛好相反了。
       再仔細(xì)看布局文件,主要是通過(guò)LinearLayoutweight屬性來(lái)控制整個(gè)豎直方向的分配,具體不再贅述。

<LinearLayout
    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:orientation="vertical"
    tools:context=".ui.activity.MainActivity">
    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/bg_color"/>
    <com.nan.cnshop.widget.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"/>
    </com.nan.cnshop.widget.FragmentTabHost>
</LinearLayout>

       在代碼當(dāng)中,先通過(guò)ID找到FragmentTabHost,然后就可以進(jìn)行配置了,具體看代碼的注釋。

public class MainActivity extendsBaseActivity {
    private FragmentTabHost tabHost;
    //用于裝載每一個(gè)分頁(yè)的Tab
    private List<Tab> tabs = null;
    @Override
    public void initView() {
        setContentView(R.layout.activity_main);
        tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    }
    @Override
    public void initData() {
        initTabs();
    }
    private void initTabs() {
        //調(diào)用setup()方法,設(shè)置FragmentManager,以及指定用于裝載Fragment的布局容器
        tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        //新建5個(gè)分頁(yè),并且添加到List當(dāng)中,便于管理,其中的圖標(biāo)我們使用了selector進(jìn)行狀態(tài)選擇,即選中的時(shí)候會(huì)變色。
        Tab home = new Tab(HomeFragment.class, R.string.home, R.drawable.selector_icon_home);
        Tab hot = new Tab(HotFragment.class, R.string.hot, R.drawable.selector_icon_hot);
        Tab cate = new Tab(CategoryFragment.class, R.string.catagory, R.drawable.selector_icon_category);
        Tab cart = new Tab(CartFragment.class, R.string.cart, R.drawable.selector_icon_cart);
        Tab mine = new Tab(MineFragment.class, R.string.mine, R.drawable.selector_icon_mine);
        tabs = new ArrayList<>();
        tabs.add(home);
        tabs.add(hot);
        tabs.add(cate);
        tabs.add(cart);
        tabs.add(mine);
        for (Tab tab : tabs) {
            //新建5個(gè)TabSpec,并且設(shè)置好它的Indicator
            TabHost.TabSpec tabSpec = tabHost.newTabSpec(getString(tab.getTitle()));
            View view = View.inflate(this, R.layout.tab_indicator, null);
            TextView tv_tab_txt = (TextView) view.findViewById(R.id.txt_indicator);
            ImageView iv_tab_icon = (ImageView) view.findViewById(R.id.icon_tab);
            tv_tab_txt.setText(tab.getTitle());
            iv_tab_icon.setImageResource(tab.getIcon());
            tabSpec.setIndicator(view);
            //把每個(gè)TabSpec添加到FragmentTabHost里面
            tabHost.addTab(tabSpec, tab.getFragment(), null);
        }
        //去掉分隔線
        tabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
        //設(shè)置當(dāng)前默認(rèn)的分頁(yè)為第一頁(yè)
        tabHost.setCurrentTab(0);
    }
    @Override
    public void initListener() {
    }
    @Override
    public void processClick(View v) {
    }
}

       需要注意的是,BaseActivity已經(jīng)繼承了FragmentActivity了。每一個(gè)TabSpecIndicator的布局文件只有一個(gè)ImageView和一個(gè)TextView,具體布局代碼如下:
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingBottom="3dp"
    android:paddingTop="3dp">
    <ImageView
        android:id="@+id/icon_tab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/txt_indicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"
        android:textColor="@color/selector_tab_text"/>
</LinearLayout>

       最需要注意的是我們的TextView的文字顏色是通過(guò)selector進(jìn)行狀態(tài)選擇的。需要注意的是,這并不是圖片,只是顏色,不能放在drawable目錄下,而應(yīng)該放在color目錄下。


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:116389 發(fā)表于 2016-5-16 12:07 | 只看該作者
樓主說(shuō)得對(duì),頂!d=====( ̄▽ ̄*)b file:///C:/Users/120/AppData/Local/Temp/SGPicFaceTpBq/4968/10087CD1.gif
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 欧美自拍一区 | 一区二区三区不卡视频 | 午夜久久久 | 国产精品久久久久久久久久久久 | 97精品久久 | 日韩三级一区 | 精品国产第一区二区三区 | 久久久国产视频 | 色综合网站 | 亚洲午夜精品一区二区三区 | 久久免费精彩视频 | 成人在线免费观看 | 在线日韩精品视频 | 午夜视频在线视频 | 99riav国产一区二区三区 | 中文字幕在线观看av | 能看的av | 国产一区二区在线看 | 日本天堂视频 | 先锋av资源在线 | 久久精品亚洲精品国产欧美kt∨ | 97国产一区二区 | 国产无人区一区二区三区 | 综合色站导航 | 日韩欧美中文 | 久久久成人一区二区免费影院 | 成人午夜网 | 国产电影一区二区三区爱妃记 | 精品1区2区 | 欧美日韩在线综合 | 精品熟人一区二区三区四区 | www国产精品 | 特黄色一级毛片 | 国产一区二区三区色淫影院 | 福利精品在线观看 | 欧美日韩精品区 | 精久久久 | 久热国产精品视频 | 久久国产综合 | 国产96在线 | 国产日韩91 |