Android - 向片段添加标签

Android - Adding tabs to a Fragment

我正在制作带有标签的片段,但我遇到了无法解决的问题。添加选项卡时,我必须使用 setContent() 设置内容,但我只能设置 Activity 或布局,我想在两个片段之间进行交易。 我有这个例外:

java.lang.IllegalArgumentException: you must specify a way to create the tab content
        at android.widget.TabHost.addTab(TabHost.java:225)
        at com.rpstudios.pokecooker.CustomFragment.onCreateView(CustomFragment.java:27)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
        at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:450)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5105)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
        at dalvik.system.NativeStart.main(Native Method)

这是抛出异常的代码:

FragmentTabHost mTabHost;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle save){

    View rootView = inflater.inflate(R.layout.tabhost_layout, container, false);


    mTabHost = (FragmentTabHost) rootView.findViewById(android.R.id.tabhost);
    mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);

    mTabHost.addTab(mTabHost.newTabSpec("TAB1").setIndicator("TAB1"));
    mTabHost.addTab(mTabHost.newTabSpec("TAB2").setIndicator("TAB2"));
    mTabHost.addTab(mTabHost.newTabSpec("TAB3").setIndicator("TAB3"));

    mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
        @Override
        public void onTabChanged(String s) {
            if(s.equals("TAB1")){

            }else if(s.equals("TAB2")){

            }else if(s.equals("TAB2")){

            }
        }
    });


    return rootView;
}

这是 tabhost_layout.xml :

<android.support.v4.app.FragmentTabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

    </LinearLayout>
</android.support.v4.app.FragmentTabHost>

根据Jesse's Answer

您应该像这样添加标签:

mTabHost.addTab(mTabHost.newTabSpec("explore").setIndicator("Explore", getResources().getDrawable(R.drawable.exploreicon)), TheAssociatedFragmentTab.class, null);

并且您需要一个 ExplorerFragment class 来覆盖 onCreateView 并为您的资源管理器选项卡扩充布局:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) 
{
  super.onCreateView(inflater, container, savedInstanceState);
  return inflater.inflate(R.layout.explorer_fragment, container,false);
}