简单使用FragmentTabHost切换Fragment

【1】布局中创建





    



        

    



【2】获取控件

public class MainActivity2 extends AppCompatActivity {

    @Bind(R.id.fl)

    FrameLayout fl;

    @Bind(R.id.iv_image)

    ImageView iv_image;

    @Bind(android.R.id.tabhost)

    FragmentTabHost tabhost;



    @Override

    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main2);

        ButterKnife.bind(this);

        //1.绑定tabhost和切换Fragment的布局

        tabhost.setup(this,getSupportFragmentManager(),R.id.fl);

        //去掉线

        tabhost.getTabWidget().setDividerDrawable(null);

        //2.创建Tab按钮

        TabHost.TabSpec allTab = tabhost.newTabSpec("all");//创建综合界面的按钮

        View view = View.inflate(this,R.layout.tab_indicator,null);

        allTab.setIndicator(view,"综合"));

        View TabSpecview = View.inflate(this,R.layout.tab_indicator,null);

        TabHost.TabSpec tweetTab = tabhost.newTabSpec("tweet");//创建动弹界面的按钮

        tweetTab.setIndicator(TabSpecview,"动弹"));



        //3.添加Tab按钮以及对应的Fragment

        tabhost.addTab(allTab, AllFragment.class,null);

        tabhost.addTab(tweetTab, TweetFragment.class,null);

        

    }

}

 

你可能感兴趣的:(简单使用FragmentTabHost切换Fragment)