【发布时间】:2017-07-25 04:13:43
【问题描述】:
不明白为什么我的 TabLayout 没有显示在我的 Fragment 中问题,请帮忙。这是我的代码:
public class ViewPagerFragment extends Fragment {
public static final String KEY_NEW_INDEX = "key_new_index";
private ViewPager mViewPager;
private TabLayout mTabLayout;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_view_pager, container, false);
final SportFragment sportFragment = new SportFragment();
Bundle bundle = new Bundle();
sportFragment.setArguments(bundle);
final BuzzFragment buzzFragment = new BuzzFragment();
bundle = new Bundle();
buzzFragment.setArguments(bundle);
mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
mTabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
mViewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
@Override
public Fragment getItem(int position) {
return position == 0 ? sportFragment : buzzFragment;
}
@Override
public CharSequence getPageTitle(int position) {
return position == 0 ? "Sport" : "Buzz";
}
@Override
public int getCount() {
return 2;
}
});
mTabLayout.setupWithViewPager(mViewPager);
return view;
}
}
这是我的 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="com.example.ideo.testfrag.FragmentUI.Fragments.ViewPagerFragment">
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabLayout"
android:background="@color/colorPrimary"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
【问题讨论】:
-
得到它,可能对其他人有帮助,只需将 tabLayout 视图放在 viewPager 视图下... ^^
-
试试下面的答案,它会起作用的......
标签: android android-fragments android-tablayout