【发布时间】:2017-04-14 01:58:03
【问题描述】:
我正在尝试在我的一个片段中添加 tablayout,但是尽管我在网络上找到了所有内容,但似乎没有任何东西可以正常工作。在其他片段中,我不想看到选项卡布局。
HomeFragment xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="@+id/home_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:tabMode="scrollable"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<android.support.v4.view.ViewPager
android:id="@+id/home_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Home片段代码:
公共类 HomeFragment 扩展 Fragment {
private TabLayout _homeTabLayout;
private ViewPager _homeViewPager;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.home_fragment,container,false);
final TabLayout tabLayout = (TabLayout) v.findViewById(R.id.home_tabs);
final ViewPager viewPager = (ViewPager) v.findViewById(R.id.home_viewpager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
adapter.addFragment(new SyncedEpisodesTabFragment(), "One");
adapter.addFragment(new AvailableEpisodesTabFragment(), "Two");
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
return v;
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter (getChildFragmentManager());
adapter.addFragment(new AvailableEpisodesTabFragment(), "PHOTOS");
adapter.addFragment(new SyncedEpisodesTabFragment(), "HI-FIVES");
viewPager.setAdapter(adapter);
}
private class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
应用栏主.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.follow.series.activities.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<!--I'm inserting the tab layout here-->
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
在这种情况下,我只是看到带有文本的 viewpager,没有标签 当我在工具栏中添加选项卡布局时,我一直在看到选项卡。
【问题讨论】:
-
我相信 tablayout 隐藏在工具栏后面。尝试使用 LinearLayout 或 RelativeLayout 包装框架布局以使其工作。或者您可以尝试在框架布局中添加
layout_below = "@id/app_bar_layout" -
框架布局在 CoordinatorLayout 内部,它不是具有 lyout_below 属性的 RelativeLayout 我也尝试用线性布局包装框架布局,但没有任何成功。