FragmentTabHost+FrameLayout

Android 你或許不知道的另外一種首頁導航佈局FragmentTabHost+FrameLayout

有問題可以加羣討論:517018699
這裏寫圖片描述

FragmentTabHost是安卓V4下的一種控件:佈局是:android.support.v4.app.FragmentTabHost,喜歡的盆友可以直接去看他的源碼,這裏我就不詳細介紹了(其實我也沒看,【丶汗】)
難度:初級 適用人羣:新手 適用地方:首頁導航功能按鈕
接下來不羅嗦,直接講解如何使用

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

    <android.support.v4.app.FragmentTabHost
        android:id="@+id/tabHost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.v4.app.FragmentTabHost>
</LinearLayout>
  • 佈局是我們經常簡單的導航欄+FrameLayout佈局樣式,就不多說了下面是精華代碼
  private void initView() {
        fragmentTabHost = (FragmentTabHost) findViewById(R.id.tabHost);
        mFragmentTitles = getResources().getStringArray(R.array.fragment_titles);                   //String[]    mFragmentTitles
        mFragmentTabHost = (FragmentTabHost) findViewById(R.id.tabhost);                            //FragmentTabHost mFragmentTabHost
        mFragmentTabHost.setup(this, getSupportFragmentManager(), R.id.frame_content);              //給tab設備對應的視圖
        for (int i = 0; i < mFragmentTitles.length; i++) {
            //給每一個tab設備圖標和文字內容
            TabSpec ts = mFragmentTabHost.newTabSpec(mFragmentTitles[i]);
            ts.setIndicator(mFragmentTitles[i]);
            switch (i) {
                case FRAGMENT_SCAN:
                    mFragmentTabHost.addTab(ts, ScanFragment.class, null);                             //按鈕添加到tab選項中
                    mFragmentTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.color.black);//設備tab按鈕背景
                    break;
                case FRAGMENT_CONNECTED:
                    mFragmentTabHost.addTab(ts, ConnectedFragment.class, null);
                    break;
            }
        updateTab(fragmentTabHost);
        }

   /**
     * 更新Tab標籤的顏色,和字體的顏色
     *
     * @param tabHost
     */
    private void updateTab(final TabHost tabHost) {
        for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
            View view = tabHost.getTabWidget().getChildAt(i);
            TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
            tv.setTextSize(16);
            tv.setTypeface(Typeface.SERIF, 2); // 設置字體和風格
            if (tabHost.getCurrentTab() == i) {//選中
                tv.setTextColor(this.getResources().getColorStateList(android.R.color.black));
            } else {//不選中
                tv.setTextColor(this.getResources().getColorStateList(android.R.color.white));
            }
        }
    }
  • 可能有的童鞋在複製的時候發現:R.array.fragment_title這是個神馬?不急往下看
        <!--在資源文件聲明導航功能的名稱-->
       <string-array name="fragment_title">
        <item>功能一</item>
        <item>功能二</item>
        <item>功能三</item>
      </string-array>
  • 我的代碼(/哭)拿去不謝。。。 題外話:當然一般在寫fragment的時候最好寫一個BaseFragement把公共的方法和方法寫在裏面,用起來爽歪歪。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章