android-fragments TabLayout未显示Android

wribegjk  于 2022-11-13  发布在  Android
关注(0)|答案(6)|浏览(198)

不明白为什么我的TabLayout没有在我的片段中显示我在片段的支持版本,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>
fdbelqdn

fdbelqdn1#

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

cx6n0qe3

cx6n0qe32#

您没有在tablayout中添加选项卡
在初始化tablayout后调用this

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

0dxa2lsx3#

您正在使用FrameLayout,因此您的视图页与您的选项卡视图重叠。相对版式也会发生同样的情况。要解决此问题,请使用线性版式或使用相对版式的below属性(用相对版式替换框架版式)。

snvhrwxg

snvhrwxg4#

我知道它有点旧,但请检查页面查看器视图是否未覆盖选项卡布局

9udxz4iz

9udxz4iz5#

有同样的问题。在我将这一行注入xml格式的TabLayout后就解决了:

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

由于您需要在AndroidManifest中设置.noActionBar来删除旧的ActionBar,如果您也是这种情况,则不容易猜测。

mznpcxlj

mznpcxlj6#

**注意:**如果您使用的是viewpager2,请不要忘记将此方法(attach)添加到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()

相关问题