2016-11-29 99 views
0

我试图在我的片段之一中添加tablayout,但尽管我在网络中找到的所有内容似乎都没有任何问题。在其他片段中,我不想看到标签布局。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> 

HomeFragment代码:

公共类HomeFragment延伸片段{

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 当我在工具栏中添加标签布局时,我总是看到标签。

+1

我相信tablayout隐藏在工具栏后面。尝试使用LinearLayout或RelativeLayout来封装framelay以使其工作。或者你可以尝试添加'layout_below =“@ id/app_bar_layout”'在framelayout – ZeroOne

+0

framelayout在CoordinatorLayout里面,它不是RelativeLayout和lyout_below属性,我也尝试用线性布局来包装帧布局,但没有任何成功。 –

回答

2

穆罕默德Hafiq Iqmal的答案,我设法将同时包含工具提示和碎片容器

<?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"> 

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

     <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" /> 




     </android.support.design.widget.AppBarLayout> 



     <FrameLayout 
      android:id="@+id/fragment_container" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 


    </LinearLayout> 


    <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> 

我没有注意到这另一个LinearLayout中来解决这个问题,但现在我有一个问题使用工具栏,它的底部下方有一个阴影,即使它位于标签布局下方。我试图在标签布局和工具栏元素中都使用elivation属性,但没有成功strange shadow

0

用于删除阴影我使用下面的代码行。像我一样,你可以在你的风格补充一点:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
<item name="android:windowContentOverlay">@null</item> 
</style> 

或写此行工具栏的属性:

<android.support.v7.widget.Toolbar 
android:windowContentOverlay="@null" 
/> 

我建议在你的框架布局使用下面的属性,因为如果没有这个属性的看法在片段可以重叠您的工具栏

app:layout_behavior="@string/appbar_scrolling_view_behavior"