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

4
我在Android中使用TabLayout时遇到了问题。由于我的最小SDK版本是10,所以我使用了AppCompat库。问题是,如果在活动首次创建时TabLayout的可见性为GONE,当我稍后将可见性设置为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中设置为VISIBLE,则可以正常工作。
我已经尝试使用.invalidate(),但它不起作用。
有人能帮助我吗?
提前感谢您的帮助。
2个回答

7

确认。这是库com.android.support:design:22.2.1中的一个bug。如果我使用com.android.support:design:22.2.0,它就可以完美地运行。这个问题将在未来的库版本中得到解决。

这是在code.google.com上的问题


0

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

升级到com.android.support:support-v13:23.1.1后出现问题,从22.2.0解决,采用以下样式:

<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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接