FragmentTabHost

首先布局

第一个FrameLayout 是用来显示内容的桢布局

下面的FragementTabHost是显示标签,标签在上面或者下面取决于布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.acer_123.fragmenttabhost.MainActivity">

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

    <android.support.v4.app.FragmentTabHost
        android:background="@android:color/darker_gray"
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.v4.app.FragmentTabHost>
</LinearLayout>
在java代码MainActivity中
 //初始化FragmentTabHost
        FragmentTabHost tabhost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        //关联tabHost和FragmeLayout
        //getSupportFragmentManager()用于兼容,需要让MainActivity继承v4包里面的FragmentActivity,如果不要兼容,可以换成getFragmentManager()
        tabhost.setup(MainActivity.this,getSupportFragmentManager(),android.R.id.tabcontent);
        for (int i = 0; i < texts.length; i++) {
            //创建Tab
            TabHost.TabSpec tabSpec = tabhost.newTabSpec("选项"+(i+1));
            //标签视图,可以自由定制
            TextView textView = new TextView(this);
            textView.setGravity(Gravity.CENTER);
            textView.setText(texts[i]);
            //在TextView上面设置图片
            textView.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.ic_launcher),null,null);
            tabSpec.setIndicator(textView);
            //把bundle信息传递到fragment去
            Bundle bundle = new Bundle();
            bundle.putString("data",texts[i]);
            //添加Tab
            tabhost.addTab(tabSpec,BaseFragment.class,bundle);

BaseFragment 继承Fragment

 public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //fragment接收通过bundle传递来的信息
        Bundle bundle = getArguments();
        if (bundle!=null){
            data = bundle.getString("data");

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (textView==null){
            textView = new TextView(getActivity());
            textView.setText(data);
            //延迟2秒执行代码,用来检验fragment的缓存
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    textView.setText("哈哈");
                }
            },2000);
        }else {
            //实现fragment缓存的代码
            ViewParent parent = textView.getParent();
            if (parent !=null && parent instanceof ViewGroup){
                ViewGroup group = (ViewGroup) parent;
                group.removeView(textView);
            }
        }

        return textView;
效果图




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值