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

标签 android visibility android-tablayout

我对 Android 中的 TabLayout 有疑问。我正在使用 AppCompat 库,因为我的最小 SDK 是 10。问题是,如果 TabLayout 在首次创建 Activity 时具有可见性 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];
    }
}

fragment :

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>

我的 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 它会完美运行。它将在库的 future 版本中解决。

Here is the issue in code.google.com

关于android - 当可见性从 GONE 变为 VISIBLE 时,TabLayout 文本消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31699961/

相关文章:

java - 使用 Retrofit2 调用 API 时无法接收数据

jquery - 限制:nth-child results to visible elements

python - 在同时运行的两个函数中访问同一对象的属性

c# - 如何使用 C# 隐藏和显示 html 模板表?

android - 无法在 ViewPager 中膨胀 recyclerView

android - 为精确的字符长度设置 EditText 宽度

android - 调用MediaPlayer.seekTo(msec)方法后,Android MediaPlayer重新启动视频

android - 如何在Android中使用jess

android - 以编程方式在 TabLayout 的选项卡之间分隔线 LinearLayout.SHOW_DIVIDER_MIDDLE

android - Unresolved reference : TabLayoutMediator