FragmentTabHost

来源:互联网 发布:金润软件成都 编辑:程序博客网 时间:2024/04/30 08:24

首先布局

第一个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;
效果图




0 0
原创粉丝点击