【问题标题】:TabLayout not showing AndroidTabLayout 未显示 Android
【发布时间】:2017-07-25 04:13:43
【问题描述】:

不明白为什么我的 TabLayout 没有显示在我的 Fragment 中问题,请帮忙。这是我的代码:

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 视图下... ^^
  • 试试下面的答案,它会起作用的......

标签: android android-fragments android-tablayout


【解决方案1】:

在我的 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) {

        }});

  }
}

MainActivity 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 代码,请告诉我。

【讨论】:

    【解决方案2】:

    你没有在tablayout中添加标签

    在你初始化tablayout之后调用它

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

    【讨论】:

      【解决方案3】:

      您正在使用 FrameLayout,因此您的 viewpager 与您的 tabview 重叠。相对布局也会发生同样的情况。要解决您的问题,请使用线性布局或使用以下相对布局属性(将框架布局替换为相对布局)。

      【讨论】:

        【解决方案4】:

        我知道它有点旧,但请检查标签布局是否未被 PageViewer 视图覆盖

        【讨论】:

          【解决方案5】:

          遇到了同样的问题。一旦我将这一行注入 xml 中的 TabLayout 就解决了:

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

          由于您需要在 AndroidManifest 中设置 .noActionBar 以摆脱旧的 ActionBar,如果您也是这种情况,那就不好猜了。

          【讨论】:

            【解决方案6】:

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

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2023-03-16
              • 2016-06-29
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多