2

何らかの理由で、デバイスの向きを変更すると、アクションバーが正しく再構築されず、タブ (スピナーとして表示されることもあります) が他のメニュー項目と重なって表示されます。

私は 4 つのタブを持っています (3 つまでは問題なく動作します) (関連する場合は Actionbarsherlock を使用します) 縦向きでは、タブにテキストの代わりに画像を使用します。

説明するスクリーンショットを次に示します。

ここに画像の説明を入力

そして、ここに私が使用するコードがあります:

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

    setContentView(R.layout.activity_main);

    // Set up the action bar.
    actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setEnabled(false);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setOffscreenPageLimit(3);

    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    }); 

    RebuildActionBar();
}

public void RebuildActionBar(){
    if(actionBar.getTabCount()>0) actionBar.removeAllTabs();
    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        if(screen_width>screen_height){
            actionBar.addTab(actionBar.newTab()
                .setText(mSectionsPagerAdapter.getPageTitle(i))
                .setTabListener(this));
        } else {
            actionBar.addTab(actionBar.newTab()
                    .setIcon(mSectionsPagerAdapter.getPageIcon(i))
                    .setTabListener(this));
        }
    }
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    RebuildActionBar();
}

マニフェストで(念のため、これは正常に機能しています):

<activity
    ...
    android:configChanges="orientation|keyboardHidden|keyboard|screenSize"
/>

1)もちろん、重なりをきれいにする必要があります。

そして 2) 万が一、スピナーを無効にするのを手伝っていただけるなら、なおさらです。両方の向きで画像を使用する必要がある場合でも、タブのみが必要です。

4

1 に答える 1