无法通过viewpager查看tablayout

时间:2018-05-27 16:08:57

标签: android android-viewpager android-tablayout

这是我的代码链接tablayout与viewpager它工作正常,但现在它无法正常工作我无法看到我的设备中的标签布局可以任何人告诉是什么导致这个问题我认为我的代码是正确的

<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:id="@+id/arootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.blipclap.creativegraphy.HomeActivity"
tools:showIn="@layout/app_bar_home">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"> 
</android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_below="@+id/tabLayout"> 
</android.support.v4.view.ViewPager>

</RelativeLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_gravity="bottom"
        app:layout_behavior=".Helper.BottomNavigationBehaviour"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimary"
        app:itemIconTint="@android:color/background_dark"
        app:itemTextColor="@android:color/background_dark"
        app:menu="@menu/bottom_navigation_menu"> 
</android.support.design.widget.BottomNavigationView>


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

在这里,我有可能适配器

public class MyFragmentAdapter extends FragmentPagerAdapter {

    private Context context;

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

    @Override
    public Fragment getItem(int position) {
        if (position == 0)
            return CategoryFragment.getInstance();
        else if (position == 1)
            return TrendingFragment.getInstance();
        else if (position == 2)
            return RecentsFragment.getInstance(context);
        else
            return null;
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "Category";
            case 1:
                return "Trending";
            case 2:
                return "Recents";
        }
        return "";
    }}

这是我使用的活动代码

 viewPager = (ViewPager)findViewById(R.id.viewPager);
            MyFragmentAdapter adapter = new 
 MyFragmentAdapter(getSupportFragmentManager(), this);
            viewPager.setAdapter(adapter);

            tabLayout = (TabLayout) findViewById(R.id.tabLayout);
            tabLayout.setupWithViewPager(viewPager);

this is the screenshot 你可以看到我无法看到手机中的标签布局可以帮助解决问题

我的布局使用bottom navigation behavior 所以坐标布局是必要的,底部导航需要出来,它应该是协调员布局的孩子

我发现标签布局隐藏在应用栏后面

<android.support.constraint.ConstraintLayout 
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"
tools:context="com.blipclap.creativegraphy.HomeActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    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"
        app:theme="@style/ToolbarColoredBackArrow" />

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

<include layout="@layout/content_home" />


</android.support.constraint.ConstraintLayout>

3 个答案:

答案 0 :(得分:0)

answer_form.html

答案 1 :(得分:0)

试试这会对你有所帮助

<?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:id="@+id/arootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.design.widget.TabLayout
        android:id="@+id/tab_host"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_red_light"> 
   </android.support.design.widget.TabLayout>
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/tab_host"
        android:layout_above="@id/design_navigation_view"> 
   </android.support.v4.view.ViewPager>
    <android.support.design.widget.BottomNavigationView
        android:id="@+id/design_navigation_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@android:color/darker_gray"> 
   </android.support.design.widget.BottomNavigationView>
    </RelativeLayout>

答案 2 :(得分:0)

解决方法是

     <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
android:layout_marginTop="?attr/actionBarSize"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

标签布局实际上在appbar下,所以我只是