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() を使用しようとしましたが、機能しません。

誰でも私を助けることができますか?

よろしくお願いします。

4

2 に答える 2

7

確認済み。ライブラリcom.android.support:design:22.2.1のバグです。com.android.support:design:22.2.0を使用すると、完全に機能します。これは、ライブラリの将来のリリースで解決される予定です。

これがcode.google.comの問題です

于 2015-07-30T07:13:20.977 に答える
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>
于 2015-11-23T05:52:49.157 に答える