Android 选项卡不显示表格布局+;寻呼机

Android 选项卡不显示表格布局+;寻呼机,android,android-viewpager,android-tablayout,Android,Android Viewpager,Android Tablayout,选项卡内容已呈现,我可以在它们之间滑动,但选项卡不显示。 activity_home.xml(注意我是如何在ViewPager下嵌套TabLayout的,如上所述) ////还尝试了以下操作,但没有任何更改 //mSlidingTabs=(TabLayout)findviewbyd(R.id.tabs); //mSlidingTabs.addTab(mSlidingTabs.newTab().setText(“选项卡1”)); //mSlidingTabs.addTab(mSlidingTab

选项卡内容已呈现,我可以在它们之间滑动,但选项卡不显示。

activity_home.xml(注意我是如何在ViewPager下嵌套TabLayout的,如上所述)

////还尝试了以下操作,但没有任何更改 //mSlidingTabs=(TabLayout)findviewbyd(R.id.tabs); //mSlidingTabs.addTab(mSlidingTabs.newTab().setText(“选项卡1”)); //mSlidingTabs.addTab(mSlidingTabs.newTab().setText(“选项卡2”)); //mSlidingTabs.setupWithViewPager(mviewpage); } }

ViewPagerAdapter.java

public class ViewPagerAdapter extends FragmentStatePagerAdapter {

    CharSequence mTabTitles[]; // This will Store the mTabTitles of the Tabs which are Going to be passed when ViewPagerAdapter is created

    // Build a Constructor and assign the passed Values to appropriate values in the class
    public ViewPagerAdapter(FragmentManager fm, CharSequence mTitles[]) {
        super(fm);

        this.mTabTitles = mTitles;
    }

    //This method return the fragment for the every position in the View Pager
    @Override
    public Fragment getItem(int position) {
        if(position == 0) // if the position is 0 we are returning the First tab
        {
            Tab1 tab1 = new Tab1();
            return tab1;
        }
        else             // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab
        {
            Tab2 tab2 = new Tab2();
            return tab2;
        }
    }

    // This method return the titles for the Tabs in the Tab Strip
    @Override
    public CharSequence getPageTitle(int position) {
        return mTabTitles[position];
    }

    // This method return the Number of tabs for the tabs Strip
    @Override
    public int getCount() {
        return mTabTitles.length;
    }
}
Tab1.java

public class Tab1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.tab_1,container,false);
        return v;
    }
}

为什么要将表格布局放在ViewPager中?。 您还需要调用tabLayout.setupWithViewPager(viewPager)

你可以试试这个

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout 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/main_activity_DrawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".HomeActivity">

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

        <include
            android:id="@+id/tool_bar"
            layout="@layout/tool_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
        </android.support.v4.view.ViewPager>
        <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:elevation="2dp" />

    </LinearLayout>
    <!-- The navigation drawer -->
    <include layout="@layout/navigation_drawer" />

</android.support.v4.widget.DrawerLayout>

如果视图实现了装饰,则ViewPager将其视为一个特殊视图,可以保留在ViewPager的顶部。这通常用于实现您自己的选项卡

有关更多信息,请访问以下链接:

此视图还支持用作ViewPager装饰的一部分,以及 可以直接添加到布局资源中的ViewPager

对于装饰:

使用ViewPager.DecorView注释进行注释的视图包括 视为查看寻呼机“装饰”的一部分。每个装饰视图的位置 可以通过其android:layout\u重力属性进行控制

我想为了更好的理解,你可以通过这个

最终结论:

你必须实施

从接口java.lang.annotation.annotation


我现在也有同样的问题

通过在
ViewPager
中嵌套
TabLayout
来遵循谷歌的文档,这样做不起作用

按照谷歌的文档,在
ViewPager
中嵌套一个
PagerTabStrip
,确实有效


解决方案
不要将
表格布局
嵌套在
查看页面
中。将其直接放置在
ViewPager

Hi@AmmarMohammed,1)我将
TabLayout
放在
ViewPager
内,因为文档中这么说(我问题中的链接)2)是的,我已经尝试过了,没有问题中提到的更改尝试将TabLayout放在AppBarLayout内的工具栏下,我使用了完全相同的结构,它对我有效。但为什么@阿默罕默德,这听起来像是一种恶作剧。你是说谷歌的文档在这里是错误的吗?试一试,老实说,我还没有读过文档,但据我记忆所及,我正在遵循一些教程。请看以下内容:谢谢,但你把
表格布局
放在
查看页面
之外,这违背了我问题的目的。我也有同样的问题。在ViewPager中使用TabLayout,就像有文档记录的一样,但我看不到选项卡。你找到解决办法了吗?
public class Tab1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.tab_1,container,false);
        return v;
    }
}
<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout 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/main_activity_DrawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".HomeActivity">

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

        <include
            android:id="@+id/tool_bar"
            layout="@layout/tool_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
        </android.support.v4.view.ViewPager>
        <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:elevation="2dp" />

    </LinearLayout>
    <!-- The navigation drawer -->
    <include layout="@layout/navigation_drawer" />

</android.support.v4.widget.DrawerLayout>