简单使用FragmentTabHost切换Fragment

版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载,违者必究。 https://blog.csdn.net/Cricket_7/article/details/89374667

【1】布局中创建

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

        android:layout_width="match_parent"

        android:layout_height="0dp"

        android:layout_weight="1" />



        <android.support.v4.app.FragmentTabHost

            android:id="@android:id/tabhost"

            android:layout_width="match_parent"

            android:layout_height="60dp"

            android:background="@color/white" />

    </FrameLayout>

</LinearLayout>

【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);

        

    }

}

猜你喜欢

转载自blog.csdn.net/Cricket_7/article/details/89374667