Android 上的 appcompat 导航选项卡上未显示片段内容

Fragment content is not shown on appcompat navigation tab on Android

我尝试在 android 上制作一个与工具栏兼容的导航选项卡。我使用 https:/github.com/codepath/android_guides/wiki/Google-Play-Style-Tabs-using-SlidingTabLayout 中的教程。过了一会儿,我设法使 UI 显示出来,显示了导航选项卡,问题是每个选项卡的内容,即片段,没有显示。它不会触发任何错误。

    class SamplePagerAdapter extends FragmentPagerAdapter {

        private String tabTitles[] = new String[] {"Tab One", "Tab Two"};
        private Context context;

        public SamplePagerAdapter(FragmentManager fm, Context context) {
            super(fm);
            this.context = context;
        }

        @Override
        public int getCount() {
            return tabTitles.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabTitles[position];
        }


        @Override
        public Fragment getItem(int position) {
            if(position == 0) {
                return new TabOneFragment();
            } else if(position == 1) {
                return new TabTwoFragment();
            }
            return null;
        }

    }

TabActivity

public class TabActivity extends ActionBarActivity {

    private SlidingTabLayout mSlidingTabLayout;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cards);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mViewPager.setAdapter(new SamplePagerAdapter(getSupportFragmentManager(), this));
        mSlidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
        mSlidingTabLayout.setDistributeEvenly(true);
        mSlidingTabLayout.setViewPager(mViewPager);
    }
}

activity_tab.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TabActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize" />

    <com.example.android.view.SlidingTabLayout
        android:id="@+id/sliding_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/toolbar" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1"
        android:background="@android:color/white"/>
</RelativeLayout>

TabOneFragment.java

public class PromoFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_tab_one, container, false);
    }
}

fragment_tab_one.xml

<FrameLayout 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" tools:context=".TabOneFragment">

    <TextView android:layout_width="match_parent" android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:gravity="center"
        android:id="@+id/text"
        android:text="@string/hello_blank_fragment"
        android:textSize="100sp" />

</FrameLayout>

TabOne 片段 UI:

显示的内容:

这是错误

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="1"
    android:background="@android:color/white"/>

改成这个..

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/sliding_tabs"
    android:background="@android:color/white"/>