当可见性从 GONE 变为 VISIBLE 时,TabLayout 文本消失

TabLayout text disappears when visibility goes from GONE to VISIBLE

Android 中的 TabLayout 有问题。我正在使用 AppCompat 库,因为我的最小 SDK 是 10。问题是,如果 TabLayout 在首次创建 activity 时可见性消失,当我稍后将可见性设置为 VISIBLE 时,选项卡标题和选项卡指示器是失踪。

这是我的 MainActivity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }  

    /**
     * Called when we press the button.
     */
    public void openTabActivity(View view) {
        Intent intent = new Intent(this, TabActivity.class);
        startActivity(intent);
    }
}

TabActivity 是这样的:

public class TabActivity extends FragmentActivity {

    MyPagerAdapter mMyPagerAdapter;
    ViewPager mViewPager;
    TabLayout mTabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab);

        // ViewPager and its adapters use support library
        // fragments, so use getSupportFragmentManager.
        mMyPagerAdapter =
                new MyPagerAdapter(
                        getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.myViewPager);
        mViewPager.setAdapter(mMyPagerAdapter);

        // Link the TabLayout with the ViewPager.
        mTabLayout = (TabLayout) findViewById(R.id.myTab);
        mTabLayout.setupWithViewPager(mViewPager);

        // If I set visibility GONE it doesn't show titles 
        // when I set it to VISIBLE again.
        // If I remove this, it works fine.
        mTabLayout.setVisibility(View.GONE);

    }

    /**
     * If the tab is visible it turn it gone, if it's gone it turn it
     * visible.
     * @param view
     */
    public void toggleTab(View view) {
        Log.d(this.getClass().toString(), "ShowTab()");
        if (mTabLayout.getVisibility() == View.VISIBLE) {
            Log.d(this.getClass().toString(), "Turning GONE");
            mTabLayout.setVisibility(View.GONE);
        } else {
            Log.d(this.getClass().toString(), "Turning VISIBLE");
            mTabLayout.setVisibility(View.VISIBLE);
        }
    }    
}

页面适配器:

public class MyPagerAdapter extends FragmentStatePagerAdapter {

    final int PAGE_COUNT = 3;
    private String tabTitles[] = new String[]{"Tab 1", "Tab 2", "Tab 3"};

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

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = new MyFragment();
        return fragment;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return tabTitles[position];
    }
}

片段:

public class MyFragment extends Fragment {

    public static MyFragment newInstance() {
        MyFragment fragment = new MyFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.my_fragment, container, false);
        return view;
    }

}

布局也很简单:

activity_main.xml

<RelativeLayout 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"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context=".MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:onClick="openTabActivity"
        android:textColor="#55F"
        android:text="Press to go to Tabs"/>

</RelativeLayout>

activity_tab.xml

<LinearLayout 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"
              android:orientation="vertical"
              android:paddingBottom="@dimen/activity_vertical_margin"
              android:paddingLeft="@dimen/activity_horizontal_margin"
              android:paddingRight="@dimen/activity_horizontal_margin"
              android:paddingTop="@dimen/activity_vertical_margin"
              tools:context=".TabActivity">

    <android.support.design.widget.TabLayout
        android:id="@+id/myTab"
        style="@style/AppTheme.Tab.NavigationTab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/lightPrimaryColor"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/myViewPager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1"/>

</LinearLayout>

my_fragment.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=".MyFragment">

    <Button
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:onClick="toggleTab"
        android:text="Press"/>

</FrameLayout>

如果我在 TabActivity.onCreate 中设置可见性 GONE,它会失败。如果它在 TabActivity.onCreate 中可见,它就可以工作。

我试过使用 .invalidate() 但它不起作用。

有人可以帮我吗?

在此先感谢您的帮助。

确认。这是库 com.android.support:design:22.2.1 中的错误。如果我使用 com.android.support:design:22.2.0 它会完美运行。将在以后的库版本中解决。

Here is the issue in code.google.com

不确定是否是同一个问题,但是当我没有设置 tabTextColortabSelectedTextColor 样式属性时,类似的事情发生在我身上。

22.2.0 升级到 com.android.support:support-v13:23.1.1 时出现故障,使用以下样式解决:

<style name="exploreTabLayout" parent="Widget.Design.TabLayout">
    <item name="tabIndicatorColor">@color/accent_teal</item>
    <item name="tabIndicatorHeight">4dp</item>
    <item name="tabTextColor">@color/bismark_blue</item>
    <item name="tabSelectedTextColor">@color/accent_teal</item>
</style>