FragmentTabHost的簡單使用

現在很多主流的應用都喜歡底部幾個導航按鈕,點擊切換不同的頁面。通常我們可以自己底部寫按鈕,然後點擊不同的按鈕自己來控制切換fragment。今天我們用fragmentTabHost來快速的實現這個功能:

<?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">

    <FrameLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"></FrameLayout>
    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#999"></android.support.v4.app.FragmentTabHost>
</LinearLayout>

這是主界面佈局,fragment顯示在frameLayout中,底部是一個FramentTabHost。


import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.example.wpf.dsyx.R;
import com.example.wpf.dsyx.fragment.Fragment1;
import com.example.wpf.dsyx.fragment.Fragment2;
import com.example.wpf.dsyx.fragment.Fragment3;
import com.example.wpf.dsyx.fragment.Fragment4;
import com.example.wpf.dsyx.tools.L;

import java.util.List;
import java.util.Map;

public class Test extends AppCompatActivity implements TabHost.OnTabChangeListener {
    private String texts[] = {"首頁", "消息", "好友", "廣場"};
    private Class fragmentArray[] = {Fragment1.class, Fragment2.class, Fragment3.class, Fragment4.class};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_test);

        FragmentTabHost fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        fragmentTabHost.setup(this, getSupportFragmentManager(), R.id.main_content);
        for (int i = 0; i < 4; i++) {
            TabHost.TabSpec spec = fragmentTabHost.newTabSpec(texts[i]).setIndicator(getView(i));
            fragmentTabHost.addTab(spec, fragmentArray[i], null);
        }
        fragmentTabHost.setOnTabChangedListener(this);
    }
    private View getView(int i) {
        //取得佈局實例
        View view = LayoutInflater.from(this).inflate(R.layout.tabcontent, null, false);
        //取得佈局對象
        ImageView imageView = (ImageView) view.findViewById(R.id.image);
        TextView textView = (TextView) view.findViewById(R.id.text);
        //設置標題
        textView.setText("sdfsdf");
        return view;
    }
    @Override
    public void onTabChanged(String tabId) {
        L.i(tabId);
    }
}

首先獲取到FragmentTabHost 控件,調用setup方法。指定fragment的容器,底部有四個導航按鈕,所以for循環四次,TabSpec spec = fragmentTabHost.newTabSpec(texts[i]).setIndicator(getView(i));
創建四個spec對象指定視圖
最後調用fragmentTabHost的addTab方法把tabspec添加到fragmentTabHost中,這樣就完成了。要監聽切換界面可以給fragmentTabHost添加setOnTabChangedListener。需要注意的是用這種方式每次切換界面都會調用oncreateView重繪。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章