利用Fragment + FragmentTabHost實現書籤導航

效果展示:
這裏寫圖片描述

FragmentTabHost與RadioGroup的區別:
* 1. FragmentTabHost的代碼來說要少。
* 2. FragmentTabHost可擴展性要強。
* 缺點:Fragment界面只能通過書籤切換,沒有ViewPager

不多說,直接上代碼:
1.MainActiviry的代碼:

public class Fragment_FragmentTabHost extends AppCompatActivity {
    private FragmentTabHost  tabHost ;
        //準備一些Fragment,設置到佈局中。
    private Class[] fragments = new Class[]{Fragment_A.class, Fragment_B.class, Fragment_C.class};
    //準備一些Fragment的數組。設置到.FragmentTabHost中
    private String[] tabText = new String[]{"日程", "發現", "設置"};
    private int[] immageResIds = new int[]{ R.drawable.game,R.drawable.appgroup,R.drawable.home};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_fragment_tabhost);
        tabHost = (FragmentTabHost) findViewById(R.id.tabHost);
        initTabHost();
    }

    private void initTabHost() {
        //步驟一:
        // 參數 1上下文  2 .管理器  3 放置Fragment的容器的id。
        tabHost .setup(this, getSupportFragmentManager(), R.id.fragment_container);
        for (int i = 0; i < fragments.length; i++) {
            //步驟二:
            //創建一個Tab對象
            //newTabSpec:傳入tab,用作一個標識
            //setIndicator傳入一個View,會顯示到tab中
            TabHost.TabSpec tab = tabHost .newTabSpec(tabText[i]).setIndicator(createTabView(i));
            //步驟三:
            //將fragment傳入到tabHost中
            //將tab放置到tabHost中
            //將tab和對應的Fragment綁定
            tabHost .addTab(tab, fragments[i], null);
        }
    }
    //方法1.
    private View createTabView(int index) {
        //創建一個tab容器
        LinearLayout tabContainer = new LinearLayout(this);
        //將容器設置的方向設置爲豎直的
        tabContainer.setOrientation(LinearLayout.VERTICAL);
        //將容器的子控件設置爲居中
        tabContainer.setGravity(Gravity.CENTER);

        ImageView imageview = new ImageView(this);
        imageview.setImageResource(immageResIds[index]);
        //通過addView方法將imageView添加到tabContainer中
        tabContainer.addView(imageview);
        TextView tv = new TextView(this);
        tv.setText(tabText[index]);
        tv.setGravity(Gravity.CENTER);
        //通過addView方法將tv添加到tabContainer中
        tabContainer.addView(tv);
        return tabContainer;
    }

    /*
    * 方法2:通過佈局管理器來創建動態View,自己選擇一種方法。
    * 
    private View createTabView2(int index) {
        LinearLayout tabContainer = (LinearLayout) getLayoutInflater().inflate(R.layout.tab_item,null);
        ImageView imageView = (ImageView) tabContainer.findViewById(R.id.iv);
        imageView.setImageResource(immageResIds[index]);

        TextView textView = (TextView) findViewById(R.id.tv_name);
        textView.setText(tabText[index]);

        return  tabContainer;

    }
*/

2 . 佈局文件代碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
   <RelativeLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"/>

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

3 .創建3個選擇器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@mipmap/bottombar_game_pressed" android:state_checked="true"/>
    <item android:drawable="@mipmap/bottombar_game_pressed" android:state_selected="true"/>
    <item android:drawable="@mipmap/bottombar_game_normal"/>
</selector>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章