使用TabLayout一个片段内;标签文字无形 [英] Using TabLayout inside a Fragment; tab text invisible

查看:296
本文介绍了使用TabLayout一个片段内;标签文字无形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在与新的支持Android设计库。我已经实现了一个 NavigationView 我的 MainActivity.java ,它使用一个FragmentManager在导航项目之间进行导航抽屉:

  getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.content,mTabLayoutFragment)
    .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
    。承诺();
 

我在片段中的一个使用 TabLayout 。这里是片段的布局:

 < android.support.design.widget.AppBarLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>

    < android.support.design.widget.TabLayout
        机器人:ID =@ + ID / tabLayout
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        应用程序:tabMode =固定
        应用程序:tabGravity =补
        应用程序:主题=@风格/ ThemeOverlay.AppCompat.Dark.ActionBar/>

    < android.support.v4.view.ViewPager
        机器人:ID =@ + ID / viewpager
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:背景=@机器人:彩色/白/>

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

和它背后的Java的code:

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

公共类TabLayoutFragment扩展片段{


    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                             捆绑savedInstanceState){
        查看inflatedView = inflater.inflate(R.layout.fragment_tablayout,集装箱,假);

        TabLayout tabLayout =(TabLayout)inflatedView.findViewById(R.id.tabLayout);
        tabLayout.addTab(tabLayout.newTab()的setText(Campusplan)。);
        tabLayout.addTab(tabLayout.newTab()的setText(Raumplan)。);
        最后ViewPager viewPager =(ViewPager)inflatedView.findViewById(R.id.viewpager);

        viewPager.setAdapter(新PagerAdapter
                (getFragmentManager(),tabLayout.getTabCount()));
        viewPager.addOnPageChangeListener(新TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(新TabLayout.OnTabSelectedListener(){
            @覆盖
            公共无效onTabSelected(TabLayout.Tab标签){
                viewPager.setCurrentItem(tab.getPosition());
            }

            @覆盖
            公共无效onTabUnselected(TabLayout.Tab标签){

            }

            @覆盖
            公共无效onTabReselected(TabLayout.Tab标签){

            }
        });

        返回inflatedView;
    }

    公共类PagerAdapter扩展FragmentStatePagerAdapter {
        INT mNumOfTabs;

        公共PagerAdapter(FragmentManager FM,NumOfTabs){
            超(FM);
            this.mNumOfTabs = NumOfTabs;
        }

        @覆盖
        公共片段的getItem(INT位置){

            开关(位置){
                情况下0:
                    TabItem1 TAB1 =新TabItem1();
                    返回TAB1;
                情况1:
                    TabItem1 TAB2 =新TabItem1();
                    返回TAB2;
                默认:
                    返回null;
            }
        }

        @覆盖
        公众诠释getCount将(){
            返回mNumOfTabs;
        }
    }
}
 

请注意 TabItem1 TabItem1 ,这是片段组成的只是一个TextView。这两个要被显示在TabLayout

现在,这个code似乎是工作,在一定程度上。下面是它的样子:

什么是奇怪的是,在我的旋转设备,一切似乎都工作得很好:

这看起来像是被要求更改配置。这是奇怪的,特别是考虑到我已经按照我的 AndroidManifest

 安卓configChanges =方向|屏幕尺寸
 

解决方案

更新:设计库v23.0.0解决了这个问题。


在这里发现了问题:<一href="https://$c$c.google.com/p/android/issues/detail?id=180462">https://$c$c.google.com/p/android/issues/detail?id=180462

简单的解决方法:

  tabLayout.post(新的Runnable(){
    @覆盖
    公共无效的run(){
        tabLayout.setupWithViewPager(viewPager);
    }
});
 

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"

解决方案

Update: Design Library v23.0.0 solves this issue.


Found the problem here: https://code.google.com/p/android/issues/detail?id=180462

Simple workaround:

tabLayout.post(new Runnable() {
    @Override
    public void run() {
        tabLayout.setupWithViewPager(viewPager);
    }
});

这篇关于使用TabLayout一个片段内;标签文字无形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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