TabLayout标签文本未显示

时间:2016-06-27 08:24:39

标签: android android-layout android-tablayout

我在TabLayout内使用Fragment来显示三个固定标签并且标签正常工作,但即使我在app:tabTextColor属性中设置public class NewFragment extends Fragment { private RecyclerView mRecyclerView; private RecyclerView.LayoutManager mLayoutManager; private RecyclerView.Adapter mAdapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View inflatedView = inflater.inflate(R.layout.new_fragment, container, false); TabLayout tabLayout = (TabLayout) inflatedView.findViewById(R.id.tabLayout); tabLayout.addTab(tabLayout.newTab().setText("Tab 1")); tabLayout.addTab(tabLayout.newTab().setText("Tab 2")); tabLayout.addTab(tabLayout.newTab().setText("Tab 3")); final ViewPager viewPager = (ViewPager) inflatedView.findViewById(R.id.viewpager); LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity()); mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); viewPager.setAdapter(new PagerAdapter(getFragmentManager(), tabLayout.getTabCount())); viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); tabLayout.setupWithViewPager(viewPager); tabLayout.setTabMode(TabLayout.MODE_FIXED); 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: return new FragmentTab(); case 1: return new FragmentTab(); case 2: return new FragmentTab(); default: return null; } } @Override public int getCount() { return mNumOfTabs; } } } 属性后它也没有显示标签文本布局它仍然不可见。

NewFragment.java

<android.support.design.widget.AppBarLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".NewFragment">

<android.support.design.widget.TabLayout
    android:id="@+id/tabLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabMode="fixed"
    app:tabTextColor="#ffffff"
    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>

newfragment.xml

 var ui = SpreadsheetApp.getUi();
 var response = ui.alert('test',test, ui.ButtonSet.OK_CANCEL);

 if (response == ui.Button.OK) {
     ui.alert('OK'); 
 } else {
     ui.alert('NOT OK')
 }

4 个答案:

答案 0 :(得分:109)

问题是您在使用setupWithViewPager()电话设置标签后调用addTab(),有效地覆盖了它们。

来自TabLayout的{​​{1}}关于setupWithViewPager()

  

此处显示的标签   布局将从ViewPager适配器的页面标题中填充。

如果您想将TabLayoutViewPager一起使用,则应覆盖getPageTitle()中的PagerAdapter(并删除addTab()来电,多余的。)

例如:

public class ExamplePagerAdapter extends FragmentStatePagerAdapter {

    // tab titles
    private String[] tabTitles = new String[]{"Tab1", "Tab2", "Tab3"};

    public ExamplePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    // overriding getPageTitle()
    @Override
    public CharSequence getPageTitle(int position) {
        return tabTitles[position];
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new Tab1Fragment();
            case 1:
                return new Tab2Fragment();
            case 2:
                return new Tab3Fragment();
            default:
                return null; // shouldn't happen
        }
    }

    @Override
    public int getCount() {
        return tabTitles.length;
    }

    // ...
}

答案 1 :(得分:8)

我在调用tabs.setupWithViewPager(viewPager)

后为每个标签添加了标签文字和图标
    viewPager.setAdapter(new MyViewAdapter(getSupportFragmentManager()));
    TabLayout tabs=(TabLayout) findViewById(R.id.tabs);
    tabs.setupWithViewPager(viewPager);
    tabs.getTabAt(0).setIcon(R.drawable.icon1);
    tabs.getTabAt(1).setIcon(R.drawable.icon2);
    tabs.getTabAt(0).setText(getResources().getText(R.string.tab1));
    tabs.getTabAt(1).setText(getResources().getText(R.string.tab2));

答案 2 :(得分:1)

我正在使用Kotlin,花了一个小时来查找这段代码中的错误。

res.headers.www-authenticate: "Bearer error=\"Invalid credentials\""

我可以毫无错误地进行编译,但是直到我意识到分配给tabLayout的 viewpager 不是 viewPager 时,标签标题才显示出来(注意大写' P')。不会产生错误,因为Kotlin将找到任何匹配的布局ID,在本例中为“ viewpager”,它是一个ViewPager,但在另一个视图上。

答案 3 :(得分:0)

谢谢Hatem Badawi

我最近开始用Java开发。之前,我在.Net中使用WPF和XAML,并且在UI设计方面存在很大差异。无论如何,我有同样的问题。我想用XML定义TabItem,而不是通过编程来实现。如您所述,此setupWithViewPager()调用会将选项卡项目的文本和图标重置为null(它使用相同的选项卡对象,只是重置其内容)。我使用了您的方法,但使其更可重用。这是我的代码:

public class TabLayoutUtil {
    public static void setupTabLayoutWithViewPager(TabLayout tabLayout, ViewPager viewPager) {
        ArrayList<Pair<CharSequence, Drawable>> tabsContentCopy= new ArrayList<>();
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            tabsContentCopy.add(new Pair<>(tab != null ? tab.getText() : null, tab != null ? tab.getIcon() : null));
        }
        tabLayout.setupWithViewPager(viewPager);
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            if (i<tabsContentCopy.size()) {
                tabLayout.getTabAt(i).setText(tabsContentCopy.get(i).first);
                tabLayout.getTabAt(i).setIcon(tabsContentCopy.get(i).second);
            }
        }
    }
}