在 Fragment 中使用 TabLayout;标签文本不可见 [英] Using TabLayout inside a Fragment; tab text invisible

查看:27
本文介绍了在 Fragment 中使用 TabLayout;标签文本不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在新的

奇怪的是,在我旋转设备后,一切似乎都运行良好:

似乎在更改配置时会调用某些东西.这很奇怪,特别是考虑到我的 AndroidManifest 中有以下内容:

android:configChanges="orientation|screenSize"

解决方案

我在新版本的 android support Design Library v23.1.1 中遇到了同样的问题.但我只是弄清楚发生了什么.

如果您的视图寻呼机有上面提到的 2 个片段,那么您可以使用以下语句设置视图寻呼机:

tabLayout.setupWithViewPager(viewPager);

您将自动拥有 2 个带有空文本的标签.所以你不应该再添加新标签.但是您必须做的是在这两个创建的标签中设置文本.

tabLayout.getTabAt(0).setText("Campusplan");tabLayout.getTabAt(1).setText("Raumplan");

结论.要使标签按您的意愿工作,您必须做的重要事情是:

tabLayout.setupWithViewPager(viewPager);tabLayout.getTabAt(0).setText("Campusplan");tabLayout.getTabAt(1).setText("Raumplan");

希望对你有帮助:)

I'm currently experimenting with various new components in the new Android Support Design library. I've implemented a NavigationView in my MainActivity.java, which uses a FragmentManager to navigate between the items in the Navigation drawer:

getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.content, mTabLayoutFragment)
    .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
    .commit();

I'm using a TabLayout in one of the fragments. Here is the fragment's layout:

<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"/>

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

And the Java code behind it:

import android.os.Bundle;
import android.support.design.widget.*;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TabLayoutFragment extends Fragment {   


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflatedView = inflater.inflate(R.layout.fragment_tablayout, container, false);

        TabLayout tabLayout = (TabLayout) inflatedView.findViewById(R.id.tabLayout);
        tabLayout.addTab(tabLayout.newTab().setText("Campusplan"));
        tabLayout.addTab(tabLayout.newTab().setText("Raumplan"));
        final ViewPager viewPager = (ViewPager) inflatedView.findViewById(R.id.viewpager);

        viewPager.setAdapter(new PagerAdapter
                (getFragmentManager(), tabLayout.getTabCount()));
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

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

            }

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

            }
        });

        return inflatedView;
    }

    public class PagerAdapter extends FragmentStatePagerAdapter {
        int mNumOfTabs;

        public PagerAdapter(FragmentManager fm, int NumOfTabs) {
            super(fm);
            this.mNumOfTabs = NumOfTabs;
        }

        @Override
        public Fragment getItem(int position) {

            switch (position) {
                case 0:
                    TabItem1 tab1 = new TabItem1();
                    return tab1;
                case 1:
                    TabItem1 tab2 = new TabItem1();
                    return tab2;
                default:
                    return null;
            }
        }

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

Please note TabItem1 and TabItem1, which are fragments consisting of nothing but a TextView. These two are to be displayed in the TabLayout.

Now, this code seems to be working, to some extent. Here is how it looks like:

What's weird is, after I rotate the device, everything seems to be working just fine:

It seems like something gets called upon a configuration change. This is weird, especially considering that I have following in my AndroidManifest:

android:configChanges="orientation|screenSize"

解决方案

I face the same problem with new version of android support Design Library v23.1.1. But I just figure it out what happening.

if your view pager has 2 fragment as your mention above, then you set your view pager with this statement:

tabLayout.setupWithViewPager(viewPager);

automatically you will have 2 tabs with empty text. So you should not add new tab anymore. But something you have to do is to set text in that two created tabs.

tabLayout.getTabAt(0).setText("Campusplan");
tabLayout.getTabAt(1).setText("Raumplan");

Conclusion. To make tabs work as you want, important thing you have to do :

tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(0).setText("Campusplan");
tabLayout.getTabAt(1).setText("Raumplan");

I hope this will be helpful for you :)

这篇关于在 Fragment 中使用 TabLayout;标签文本不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆