Tabs using android.support.v7.app.ActionBar

2019-02-15 08:05发布

问题:

I recently updated my android app to use the v7 support for actionbar and actionbar tabs on android 2.3. After updating the code I ran the app on a 4.2 device and a emulated 2.3 device. The actionbar and tabs show up fine on the real hardware, but not on the emulated device. Only the first fragment shows up and the left and top of the view are clipped. Here is my main actionbaractivity.

  package com.rmsanger.minneapolishiltongargeninn;

  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentTransaction;
  import android.os.Bundle;
  import android.support.v7.app.ActionBar;
  import android.support.v7.app.ActionBar.Tab;
  import android.support.v7.app.ActionBarActivity;

  public class MainActivity extends ActionBarActivity {



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

    android.support.v7.app.ActionBar actionBar = getSupportActionBar();


    //actionBar.setDisplayShowTitleEnabled(false);

    Tab tab = actionBar.newTab();   


    String label2 = getResources().getString(R.string.informationtab_label);
    tab = actionBar.newTab();
    tab.setText(label2);
    TabListener<InformationFragment> tl2 = new TabListener<InformationFragment>(this, label2, InformationFragment.class);
    tab.setTabListener(tl2);
    actionBar.addTab(tab);

    String label3 = getResources().getString(R.string.diningtab_label);
    tab = actionBar.newTab();
    tab.setText(label3);
    TabListener<DiningFragment> tl3 = new TabListener<DiningFragment>(this, label3, DiningFragment.class);
    tab.setTabListener(tl3);
    actionBar.addTab(tab);

    String label6 = getResources().getString(R.string.localattractionstab_label);
    tab = actionBar.newTab();
    tab.setText(label6);
    TabListener<LocalAttractionsFragment> tl6 = new TabListener<LocalAttractionsFragment>(this, label6, LocalAttractionsFragment.class);
    tab.setTabListener(tl6);
    actionBar.addTab(tab);


    String label7 = getResources().getString(R.string.localeventstab_label);
    tab = actionBar.newTab();
    tab.setText(label7);
    TabListener<LocalEventsFragment> tl7 = new TabListener<LocalEventsFragment>(this, label7, LocalEventsFragment.class);
    tab.setTabListener(tl7);
    actionBar.addTab(tab);

    String label5 = getResources().getString(R.string.faqtab_label);
    tab = actionBar.newTab();
    tab.setText(label5);
    TabListener<FAQFragment> tl5 = new TabListener<FAQFragment>(this, label5, FAQFragment.class);
    tab.setTabListener(tl5);
    actionBar.addTab(tab);

    String label8 = getResources().getString(R.string.dealstab_label);
    tab = actionBar.newTab();
    tab.setText(label8);
    TabListener<DealsFragment> tl8 = new TabListener<DealsFragment>(this, label8, DealsFragment.class);
    tab.setTabListener(tl8);
    actionBar.addTab(tab);

    String label4 = getResources().getString(R.string.socialmediatab_label);
    tab = actionBar.newTab();
    tab.setText(label4);
    TabListener<SocialMediaFragment> tl4 = new TabListener<SocialMediaFragment>(this, label4, SocialMediaFragment.class);
    tab.setTabListener(tl4);
    actionBar.addTab(tab);


    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.show();

    if (savedInstanceState != null) {
        actionBar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
    }   

}


@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("tab", getSupportActionBar().getSelectedNavigationIndex());
}   

private class TabListener<T extends Fragment> implements
    ActionBar.TabListener {
    private Fragment mFragment;
    private final ActionBarActivity mActivity;
    private final String mTag;
    private final Class<T> mClass;

    /**
     * Constructor used each time a new tab is created.
     * 
     * @param activity
     *            The host Activity, used to instantiate the fragment
     * @param tag
     *            The identifier tag for the fragment
     * @param clz
     *            The fragment's Class, used to instantiate the fragment
     */
    public TabListener(ActionBarActivity activity, String tag, Class<T> clz) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {

        mFragment = mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
        // Check if the fragment is already initialized
        if (mFragment == null) {
            // If not, instantiate and add it to the activity
            mFragment = Fragment.instantiate(mActivity, mClass.getName());
            ft.add(android.R.id.content, mFragment, mTag);
        } else {
            // If it exists, simply attach it in order to show it
            ft.attach(mFragment);
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        if (mFragment != null) {
            // Detach the fragment, because another one is being attached
            ft.detach(mFragment);
        }
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // User selected the already selected tab. Usually do nothing.
    }
}


  }

* UPDATE I replaced

     ft.add(android.R.id.content, mFragment, mTag);

with

     ft.add(R.id.fragView, mFragment, mTag);

where fragView is just a relative layout and the tabs are now displayed. However the view is still clipped on the top and left sides.

回答1:

Updated to use the method described here and solved the layout problems.

ActionBar with support library and Fragments overlay content