TabLayout不显示Android。

7

不明白为什么我的 TabLayout 没有显示在 Fragment 中,我的支持版本为 fragment、SDK 版本为 25,在一切正常运行的情况下无法看到 TabLayout,在搜索后没有找到关于我的问题的任何信息,请帮忙解决。下面是我的代码:

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视图下面即可... ^^ - samuel zaffran
尝试下面的答案,它会起作用的... - rafsanahmad007
6个回答

1

注意: 如果您正在使用viewpager2,请不要忘记在TabLayoutMediator的结尾添加此方法(attach)

    val viewpager = findViewById<ViewPager2>(R.id.viewpager_main)
    val tablayout = findViewById<TabLayout>(R.id.tablayout_main)

    viewpager.adapter = pagerTabAdapter
    TabLayoutMediator(tablayout , viewpager , true) { tab , index ->
        val title = titleList[index]
        tab.text  = title
    }.attach()

0

我知道这有点老了,但请检查选项卡布局是否被页面视图覆盖。


0
你正在使用FrameLayout,所以你的ViewPager重叠了你的TabView。同样的问题也会出现在RelativeLayout中。为了解决你的问题,使用LinearLayout或使用RelativeLayout的以下属性(将FrameLayout替换为RelativeLayout)。

0

遇到了同样的问题。在xml中将此行代码注入TabLayout后问题得以解决:

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

如果你需要在AndroidManifest中设置.noActionBar来摆脱旧的ActionBar,如果这也是你的情况,那么很难猜到。


0
在我的MainActivity承载了几个选项卡的情况下,我会这样做:
MainActivity JAVA:
    public class MainActivity extends AppCompatActivity {

TabLayout tabLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    int position = 0;

    tabLayout = (TabLayout) findViewById(R.id.tab_layout);

    tabLayout.addTab(tabLayout.newTab().setText("HOME"));
    tabLayout.addTab(tabLayout.newTab().setText("Search"));
    tabLayout.addTab(tabLayout.newTab().setText("PROFILE"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final PagerAdapter adapter = new PagerAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.setOffscreenPageLimit(2);
    viewPager.setCurrentItem(position);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {

            viewPager.setCurrentItem(tab.getPosition());

            if(tab.getPosition() == 0){

                InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                inputMethodManager.hideSoftInputFromWindow(viewPager.getWindowToken(), 0);

            }
            else if(tab.getPosition() == 1){

                InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                inputMethodManager.hideSoftInputFromWindow(viewPager.getWindowToken(), 0);

            }
            else if(tab.getPosition() == 2){

                InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                inputMethodManager.hideSoftInputFromWindow(viewPager.getWindowToken(), 0);

            }

        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }});

  }
}

主活动 XML

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
tools:context="celebritytime.com.celeberitytime.MainActivity">


<FrameLayout
    android:id="@+id/container_id"
    android:layout_width="0dp"
    android:layout_height="match_parent">

</FrameLayout>

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:tag="NationFragmentTag"
    android:showDividers="none"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="#020202"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/tab_layout"/>

</RelativeLayout>

如果你需要ViewPager的代码,请告诉我。


0

你没有在 tablayout 中添加选项卡

在初始化 tablayout 后调用此函数

    //Adding the tabs using addTab() method
    tabLayout.addTab(tabLayout.newTab().setText("Sport"));
    tabLayout.addTab(tabLayout.newTab().setText("Buzz"));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接